Skip to content

Commit 6896c6d

Browse files
author
Hugo Hamon
committed
[Utf8] added new AsciiTestCase class.
1 parent 02644e4 commit 6896c6d

File tree

13 files changed

+1998
-1275
lines changed

13 files changed

+1998
-1275
lines changed

src/Symfony/Component/Utf8/Bytes.php

Lines changed: 159 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,6 @@ public function trimRight(string $charsList = null): self
145145
return $result;
146146
}
147147

148-
public function startsWith(string $prefix, int $offset = 0): bool
149-
{
150-
if ($offset < 0) {
151-
throw new InvalidArgumentException(sprintf('Offset must be greater than or equal 0, %s given.', $offset));
152-
}
153-
154-
return $offset === strpos($this->string, $prefix, $offset);
155-
}
156-
157-
public function endsWith(string $suffix): bool
158-
{
159-
$suffix = (string) $suffix;
160-
161-
return substr($this->string, -strlen($suffix)) === $suffix;
162-
}
163-
164148
public function getIterator(int $maxChunkLength = 1): \Generator
165149
{
166150
if ($maxChunkLength < 1) {
@@ -203,4 +187,163 @@ public function toGraphemes(): Graphemes
203187
{
204188
return Graphemes::fromString($this->string);
205189
}
190+
191+
public function indexOf(string $needle, int $offset = 0)
192+
{
193+
if ('' === $needle) {
194+
return null;
195+
}
196+
197+
if (PHP_VERSION_ID < 70100 && $offset < 0) {
198+
return null;
199+
}
200+
201+
$result = strpos($this->string, $needle, $offset);
202+
203+
return false === $result ? null : $result;
204+
}
205+
206+
public function indexOfIgnoreCase(string $needle, int $offset = 0)
207+
{
208+
if ('' === $needle) {
209+
return null;
210+
}
211+
212+
if (PHP_VERSION_ID < 70100 && $offset < 0) {
213+
return null;
214+
}
215+
216+
$result = stripos($this->string, $needle, $offset);
217+
218+
return false === $result ? null : $result;
219+
}
220+
221+
public function lastIndexOf(string $needle, int $offset = 0)
222+
{
223+
if ('' === $needle) {
224+
return null;
225+
}
226+
227+
$result = strrpos($this->string, $needle, $offset);
228+
229+
return false === $result ? null : $result;
230+
}
231+
232+
public function lastIndexOfIgnoreCase(string $needle, int $offset = 0)
233+
{
234+
if ('' === $needle) {
235+
return null;
236+
}
237+
238+
$result = strripos($this->string, $needle, $offset);
239+
240+
return false === $result ? null : $result;
241+
}
242+
243+
public function substringOf(string $needle, int $offset = 0)
244+
{
245+
if ('' === $needle) {
246+
return null;
247+
}
248+
249+
$haystack = $offset > 0 ? substr($this->string, $offset) : $this->string;
250+
251+
$result = strstr($haystack, $needle);
252+
253+
return false === $result ? null : $result;
254+
}
255+
256+
public function substringOfIgnoreCase(string $needle, int $offset = 0)
257+
{
258+
if ('' === $needle) {
259+
return null;
260+
}
261+
262+
$haystack = $offset > 0 ? substr($this->string, $offset) : $this->string;
263+
264+
$result = stristr($haystack, $needle);
265+
266+
return false === $result ? null : $result;
267+
}
268+
269+
public function lastSubstringOf(string $needle, int $offset = 0)
270+
{
271+
if ('' === $needle) {
272+
return null;
273+
}
274+
275+
$haystack = $offset > 0 ? substr($this->string, $offset) : $this->string;
276+
277+
$result = strrchr($haystack, $needle);
278+
279+
return false === $result ? null : $result;
280+
}
281+
282+
public function lastSubstringOfIgnoreCase(string $needle, int $offset = 0)
283+
{
284+
if ('' === $needle) {
285+
return null;
286+
}
287+
288+
if (false === $offset = strripos($this->string, $needle, $offset)) {
289+
return null;
290+
}
291+
292+
return substr($this->string, $offset);
293+
}
294+
295+
public static function compare(string $a, string $b): int
296+
{
297+
return strcmp($a, $b);
298+
}
299+
300+
public static function compareIgnoreCase(string $a, string $b): int
301+
{
302+
return strcasecmp($a, $b);
303+
}
304+
305+
public static function compareNatural(string $a, string $b): int
306+
{
307+
return strnatcmp($a, $b);
308+
}
309+
310+
public static function compareNaturalIgnoreCase(string $a, string $b): int
311+
{
312+
return strnatcasecmp($a, $b);
313+
}
314+
315+
/**
316+
* @return static
317+
*/
318+
public function reverse()
319+
{
320+
$result = new self();
321+
$result->string = strrev($this->string);
322+
323+
return $result;
324+
}
325+
326+
public function width(): int
327+
{
328+
return strlen($this->string);
329+
}
330+
331+
public function replace($from, $to, int $offset = 0, int $length = null, int &$count = null)
332+
{
333+
}
334+
335+
public function replaceIgnoreCase($from, $to, int $offset = 0, int $length = null, int &$count = null)
336+
{
337+
}
338+
339+
/**
340+
* @return static
341+
*/
342+
public function toUpperCaseFirst($allWords = false)
343+
{
344+
$result = clone $this;
345+
$result->string = $allWords ? ucwords($this->string) : ucfirst($this->string);
346+
347+
return $result;
348+
}
206349
}

src/Symfony/Component/Utf8/CodePoints.php

Lines changed: 178 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,6 @@ public function trimRight(string $charsList = null): self
163163
return $result;
164164
}
165165

166-
public function startsWith(string $prefix, int $offset = 0): bool
167-
{
168-
if ($offset < 0) {
169-
throw new InvalidArgumentException(sprintf('Offset must be greater than or equal 0, %s given.', $offset));
170-
}
171-
172-
return $offset === mb_strpos($this->string, $prefix, $offset, 'UTF-8');
173-
}
174-
175-
public function endsWith(string $suffix): bool
176-
{
177-
$suffix = (string) $suffix;
178-
179-
return mb_substr($this->string, -mb_strlen($suffix), null, 'UTF-8') === $suffix;
180-
}
181-
182166
public function getIterator(int $maxChunkLength = 1): \Generator
183167
{
184168
if ($maxChunkLength < 1) {
@@ -225,4 +209,182 @@ public function toGraphemes(): Graphemes
225209
{
226210
return Graphemes::fromString($this->string);
227211
}
212+
213+
public function indexOf(string $needle, int $offset = 0)
214+
{
215+
if ('' === $needle) {
216+
return null;
217+
}
218+
219+
if (PHP_VERSION_ID < 70100 && $offset < 0) {
220+
return null;
221+
}
222+
223+
$result = mb_strpos($this->string, $needle, $offset > 0 ? $offset : 0);
224+
225+
return false === $result ? null : $result;
226+
}
227+
228+
public function indexOfIgnoreCase(string $needle, int $offset = 0)
229+
{
230+
if ('' === $needle) {
231+
return null;
232+
}
233+
234+
if (PHP_VERSION_ID < 70100 && $offset < 0) {
235+
return null;
236+
}
237+
238+
$result = mb_stripos($this->string, $needle, $offset);
239+
240+
return false === $result ? null : $result;
241+
}
242+
243+
public function lastIndexOf(string $needle, int $offset = 0)
244+
{
245+
if ('' === $needle) {
246+
return null;
247+
}
248+
249+
$result = mb_strrpos($this->string, $needle, $offset);
250+
251+
return false === $result ? null : $result;
252+
}
253+
254+
public function lastIndexOfIgnoreCase(string $needle, int $offset = 0)
255+
{
256+
if ('' === $needle) {
257+
return null;
258+
}
259+
260+
$result = mb_strripos($this->string, $needle, $offset);
261+
262+
return false === $result ? null : $result;
263+
}
264+
265+
public function substringOf(string $needle, int $offset = 0)
266+
{
267+
if ('' === $needle) {
268+
return null;
269+
}
270+
271+
$haystack = $offset > 0 ? mb_substr($this->string, $offset, null, 'UTF-8') : $this->string;
272+
273+
$result = mb_strstr($haystack, $needle, null, 'UTF-8');
274+
275+
return false === $result ? null : $result;
276+
}
277+
278+
public function substringOfIgnoreCase(string $needle, int $offset = 0)
279+
{
280+
if ('' === $needle) {
281+
return null;
282+
}
283+
284+
$haystack = $offset > 0 ? mb_substr($this->string, $offset, null, 'UTF-8') : $this->string;
285+
286+
$result = mb_stristr($haystack, $needle, null, 'UTF-8');
287+
288+
return false === $result ? null : $result;
289+
}
290+
291+
public function lastSubstringOf(string $needle, int $offset = 0)
292+
{
293+
if ('' === $needle) {
294+
return null;
295+
}
296+
297+
$haystack = $offset > 0 ? mb_substr($this->string, $offset, null, 'UTF-8') : $this->string;
298+
299+
$result = mb_strrchr($haystack, $needle, null, 'UTF-8');
300+
301+
return false === $result ? null : $result;
302+
}
303+
304+
public function lastSubstringOfIgnoreCase(string $needle, int $offset = 0)
305+
{
306+
if ('' === $needle) {
307+
return null;
308+
}
309+
310+
$haystack = $offset > 0 ? mb_substr($this->string, $offset, null, 'UTF-8') : $this->string;
311+
312+
$result = mb_strrichr($haystack, $needle, null, 'UTF-8');
313+
314+
return false === $result ? null : $result;
315+
}
316+
317+
public static function compare(string $a, string $b): int
318+
{
319+
return strcmp($a, $b);
320+
}
321+
322+
public static function compareIgnoreCase(string $a, string $b): int
323+
{
324+
return strcasecmp($a, $b);
325+
}
326+
327+
public static function compareNatural(string $a, string $b): int
328+
{
329+
return strnatcmp($a, $b);
330+
}
331+
332+
public static function compareNaturalIgnoreCase(string $a, string $b): int
333+
{
334+
return strnatcasecmp($a, $b);
335+
}
336+
337+
/**
338+
* @return static
339+
*/
340+
public function reverse()
341+
{
342+
$result = new self();
343+
$result->string = implode('', array_reverse(iterator_to_array($this)));
344+
345+
return $result;
346+
}
347+
348+
public function width(): int
349+
{
350+
return mb_strwidth($this->string, 'UTF-8');
351+
}
352+
353+
public function replace($from, $to, int $offset = 0, int $length = null, int &$count = null)
354+
{
355+
// TODO: Implement replace() method.
356+
}
357+
358+
public function replaceIgnoreCase($from, $to, int $offset = 0, int $length = null, int &$count = null)
359+
{
360+
// TODO: Implement replaceIgnoreCase() method.
361+
}
362+
363+
/**
364+
* @return static
365+
*/
366+
public function toUpperCaseFirst($allWords = false)
367+
{
368+
$ucwords = function (string $string) {
369+
return preg_replace_callback(
370+
"/\b(.)/u",
371+
function (array $matches) {
372+
return mb_convert_case($matches[1], MB_CASE_TITLE, 'UTF-8');
373+
},
374+
$string
375+
);
376+
};
377+
378+
if ($allWords) {
379+
$string = $ucwords($this->string);
380+
} else {
381+
$capitalLetter = mb_substr($this->string, 0, 1, 'UTF-8');
382+
$string = $ucwords($capitalLetter).substr($this->string, strlen($capitalLetter));
383+
}
384+
385+
$result = clone $this;
386+
$result->string = $string;
387+
388+
return $result;
389+
}
228390
}

0 commit comments

Comments
 (0)