Skip to content

Commit d77f1da

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

File tree

12 files changed

+1586
-1330
lines changed

12 files changed

+1586
-1330
lines changed

src/Symfony/Component/Utf8/Bytes.php

Lines changed: 139 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,143 @@ 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+
// TODO: Implement substringOf() method.
246+
}
247+
248+
public function substringOfIgnoreCase(string $needle, int $offset = 0)
249+
{
250+
// TODO: Implement substringOfIgnoreCase() method.
251+
}
252+
253+
public function lastSubstringOf(string $needle, int $offset = 0)
254+
{
255+
// TODO: Implement lastSubstringOf() method.
256+
}
257+
258+
public function lastSubstringOfIgnoreCase(string $needle, int $offset = 0)
259+
{
260+
// TODO: Implement lastSubstringOfIgnoreCase() method.
261+
}
262+
263+
public static function compare($a, $b): int
264+
{
265+
// TODO: Implement compare() method.
266+
}
267+
268+
public static function compareNatural($a, $b): int
269+
{
270+
// TODO: Implement compareNatural() method.
271+
}
272+
273+
public static function compareIgnoreCase($a, $b): int
274+
{
275+
// TODO: Implement compareIgnoreCase() method.
276+
}
277+
278+
public static function compareNaturalIgnoreCase($a, $b): int
279+
{
280+
// TODO: Implement compareNaturalIgnoreCase() method.
281+
}
282+
283+
/**
284+
* @return static
285+
*/
286+
public function reverse()
287+
{
288+
$result = new self();
289+
$result->string = strrev($this->string);
290+
291+
return $result;
292+
}
293+
294+
public function width(): int
295+
{
296+
return strlen($this->string);
297+
}
298+
299+
public function replace(
300+
$from,
301+
$to,
302+
int $offset = 0,
303+
int $length = null,
304+
int &$count = null
305+
) {
306+
// TODO: Implement replace() method.
307+
}
308+
309+
public function replaceIgnoreCase(
310+
$from,
311+
$to,
312+
int $offset = 0,
313+
int $length = null,
314+
int &$count = null
315+
) {
316+
// TODO: Implement replaceIgnoreCase() method.
317+
}
318+
319+
/**
320+
* @return static
321+
*/
322+
public function toUpperCaseFirst($allWords = false)
323+
{
324+
$result = clone $this;
325+
$result->string = $allWords ? ucwords($this->string) : ucfirst($this->string);
326+
327+
return $result;
328+
}
206329
}

src/Symfony/Component/Utf8/CodePoints.php

Lines changed: 156 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,160 @@ 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+
// TODO: Implement substringOf() method.
268+
}
269+
270+
public function substringOfIgnoreCase(string $needle, int $offset = 0)
271+
{
272+
// TODO: Implement substringOfIgnoreCase() method.
273+
}
274+
275+
public function lastSubstringOf(string $needle, int $offset = 0)
276+
{
277+
// TODO: Implement lastSubstringOf() method.
278+
}
279+
280+
public function lastSubstringOfIgnoreCase(string $needle, int $offset = 0)
281+
{
282+
// TODO: Implement lastSubstringOfIgnoreCase() method.
283+
}
284+
285+
public static function compare($a, $b): int
286+
{
287+
// TODO: Implement compare() method.
288+
}
289+
290+
public static function compareNatural($a, $b): int
291+
{
292+
// TODO: Implement compareNatural() method.
293+
}
294+
295+
public static function compareIgnoreCase($a, $b): int
296+
{
297+
// TODO: Implement compareIgnoreCase() method.
298+
}
299+
300+
public static function compareNaturalIgnoreCase($a, $b): int
301+
{
302+
// TODO: Implement compareNaturalIgnoreCase() method.
303+
}
304+
305+
/**
306+
* @return static
307+
*/
308+
public function reverse()
309+
{
310+
$result = new self();
311+
$result->string = implode('', array_reverse(iterator_to_array($this)));
312+
313+
return $result;
314+
}
315+
316+
public function width(): int
317+
{
318+
return mb_strwidth($this->string, 'UTF-8');
319+
}
320+
321+
public function replace(
322+
$from,
323+
$to,
324+
int $offset = 0,
325+
int $length = null,
326+
int &$count = null
327+
) {
328+
// TODO: Implement replace() method.
329+
}
330+
331+
public function replaceIgnoreCase(
332+
$from,
333+
$to,
334+
int $offset = 0,
335+
int $length = null,
336+
int &$count = null
337+
) {
338+
// TODO: Implement replaceIgnoreCase() method.
339+
}
340+
341+
/**
342+
* @return static
343+
*/
344+
public function toUpperCaseFirst($allWords = false)
345+
{
346+
$ucwords = function (string $string) {
347+
return preg_replace_callback(
348+
"/\b(.)/u",
349+
function (array $matches) {
350+
return mb_convert_case($matches[1], MB_CASE_TITLE, 'UTF-8');
351+
},
352+
$string
353+
);
354+
};
355+
356+
if ($allWords) {
357+
$string = $ucwords($this->string);
358+
} else {
359+
$capitalLetter = mb_substr($this->string, 0, 1, 'UTF-8');
360+
$string = $ucwords($capitalLetter).substr($this->string, strlen($capitalLetter));
361+
}
362+
363+
$result = clone $this;
364+
$result->string = $string;
365+
366+
return $result;
367+
}
228368
}

0 commit comments

Comments
 (0)