Skip to content

fix: unicode-aware checks, safe regex, new helpers - #17

Merged
oniice merged 24 commits into
mainfrom
develop
Nov 27, 2026
Merged

oniice merged 24 commits into
mainfrom
develop

Conversation

@oniice

@oniice oniice commented Nov 27, 2026

Copy link
Copy Markdown
Member

Security, Unicode and feature work from the utilities review.

  • Escaped slug separators; literal replace()/removeRepeating() (no backreferences, empty needle is a no-op)
  • Unicode-aware isAlpha/isAlphaNumeric/isNumeric, toAlpha and replaceNon*; isNumeric('0') is true
  • Multibyte-safe beginsWith/endsWith, character offsets in occurrences, matches() throws on invalid patterns
  • Consistent non-ASCII case conversion, explode keeps '0', equals() compares string values
  • New truncate, mask, excerpt, words, lines, isUpper, isLower, isUuid, ascii
  • Cached length and fewer mb_substr calls
  • Docs signatures fixed and safety notes for clean()/minimise(); Pint applied

- preg_quote slug separators
- Make replace() and removeRepeating() literal and safe on empty input
isAlpha, isAlphaNumeric and isNumeric now use \p{L}/\p{N} with /u. isNumeric('0') returns true. Whitespace handling is unchanged.
Case-insensitive comparison now uses mb_strtolower instead of strcasecmp. endsWith returns false when the needle is longer than the string.
Use mb_strpos so positions are character offsets, and return an empty array for an empty needle.
matches() threw nothing and returned false for an invalid regex. It now throws InvalidArgumentException using preg_last_error_msg().
Fix at, format, value, matches, toSentence, isAlphaNumeric and ensureBeginsWith headings, anchors and examples. Document make, encoding and setEncoding.
- use \p{L}/\p{N} with /u in replaceNonAlpha, replaceNonAlphanumeric and replaceNonNumeric
- toAlpha, toAlphanumeric and toNumeric inherit the change, keeping é etc.
- add tests and update docs
- use \p{Lu}/\p{Ll} with /u in camel, snake, studly and pascal case
- replace mb_ereg_* in toSnakeCase with preg and /u
- pass the encoding to mb_ucfirst/mb_lcfirst
- add tests and update docs
- filter only empty strings after trimming, so '0' is preserved
- whitespace-only segments are now dropped as well
- add tests and update docs
- cast both sides to string so Stringable and Utility inputs match
- add tests, and fix the equals docs example
- state clean() is not an XSS defence
- state minimise() is not safe for untrusted or complex JS
- fix the clean and minimise doc examples
- cache length() per instance, reset by setEncoding
- compute the contains haystack once, skipping mb_substr at offset 0
- add a test for length after an encoding change
- truncate to a character limit, multibyte-safe
- append a configurable ending only when shortened
- add tests and docs
- mask a character range with a repeated character
- support negative index and optional length
- add tests and docs
- extract a phrase with surrounding context, case-insensitive
- add omission where the string was cut, multibyte-safe
- add tests and docs
- limit the string to a number of words
- append a configurable ending only when shortened
- add tests and docs
- split the string into lines on any newline sequence
- keep empty lines, return an empty array for an empty string
- add tests and docs
- true when the string has cased letters and none are uppercase
- multibyte-safe, empty and caseless strings are false
- add tests and docs
- true when the string has cased letters and none are lowercase
- multibyte-safe, empty and caseless strings are false
- add tests and docs
- validate the 8-4-4-4-12 hexadecimal UUID format, case-insensitive
- reject trailing newlines and surrounding whitespace
- add tests and docs
- transliterate to ASCII with intl Transliterator when available
- fall back to iconv for Latin accents without intl
- add tests and docs
- order class elements in the new method tests
- import Stringable in EqualsTest
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ Complexity Δ
src/Utility.php 100.00% <100.00%> (ø) 204.00 <30.00> (+34.00)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- cover matches() throwing on an invalid pattern
- cover occurrences() with an empty needle
- cover removeRepeating() with an empty value
- Move the intl lookup into a protected transliterator() method
- Test the iconv fallback by simulating a platform without intl
@oniice
oniice merged commit ef520f9 into main Nov 27, 2026
14 checks passed
@oniice
oniice deleted the develop branch November 27, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant