Skip to content

[String] bytesAt() and codePointsAt() #33940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0
return $this->slice(0, $i);
}

/**
* @return int[]
*/
public function bytesAt(int $offset): array
{
$str = $this->slice($offset, 1);

return '' === $str->string ? [] : array_values(unpack('C*', $str->string));
}

/**
* @return static
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,14 @@ public function camel(): parent
return $str;
}

public function codePoint(int $offset = 0): ?int
/**
* @return int[]
*/
public function codePointsAt(int $offset): array
{
$str = $offset ? $this->slice($offset, 1) : $this;
$str = $this->slice($offset, 1);

return '' === $str->string ? null : mb_ord($str->string);
return '' === $str->string ? [] : array_map('mb_ord', preg_split('//u', $str->string, -1, PREG_SPLIT_NO_EMPTY));
}

public function folded(bool $compat = true): parent
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public static function fromRandom(int $length = 16): self
return new static(substr($string, 0, $length));
}

public function byteCode(int $offset = 0): ?int
public function bytesAt(int $offset): array
{
$str = $offset ? $this->slice($offset, 1) : $this;
$str = $this->string[$offset] ?? '';

return '' === $str->string ? null : \ord($str->string);
return '' === $str ? [] : [\ord($str)];
}

public function append(string ...$suffix): parent
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/String/CodePointString.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function chunk(int $length = 1): array
return $chunks;
}

public function codePointsAt(int $offset): array
{
$str = $offset ? $this->slice($offset, 1) : $this;

return '' === $str->string ? [] : [mb_ord($str->string)];
}

public function endsWith($suffix): bool
{
if ($suffix instanceof AbstractString) {
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ public function testCreateFromEmptyString()
$this->assertTrue($instance->isEmpty());
}

/**
* @dataProvider provideBytesAt
*/
public function testBytesAt(array $expected, string $string, int $offset, int $form = null)
{
$instance = static::createFromString($string);
$instance = $form ? $instance->normalize($form) : $instance;

$this->assertSame($expected, $instance->bytesAt($offset));
}

public static function provideBytesAt(): array
{
return [
[[], '', 0],
[[], 'a', 1],
[[0x62], 'abc', 1],
[[0x63], 'abcde', -3],
];
}

/**
* @dataProvider provideWrap
*/
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ public function provideCreateFromCodePoint(): array
];
}

public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3, 0xA4], 'Späßchen', 2],
[[0xC3, 0x9F], 'Späßchen', -5],
]
);
}

/**
* @dataProvider provideCodePointsAt
*/
public function testCodePointsAt(array $expected, string $string, int $offset, int $form = null)
{
$instance = static::createFromString($string);
$instance = $form ? $instance->normalize($form) : $instance;

$this->assertSame($expected, $instance->codePointsAt($offset));
}

public static function provideCodePointsAt(): array
{
return [
[[], '', 0],
[[], 'a', 1],
[[0x53], 'Späßchen', 0],
[[0xE4], 'Späßchen', 2],
[[0xDF], 'Späßchen', -5],
[[0x260E], '☢☎❄', 1],
];
}

public static function provideLength(): array
{
return [
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/String/Tests/ByteStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ protected static function createFromString(string $string): AbstractString
return new ByteString($string);
}

public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3], 'Späßchen', 2],
[[0x61], "Spa\u{0308}ßchen", 2],
[[0xCC], "Spa\u{0308}ßchen", 3],
[[0xE0], 'नमस्ते', 6],
]
);
}

public static function provideLength(): array
{
return array_merge(
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/String/Tests/CodePointStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,28 @@ public static function provideLength(): array
]
);
}

public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0x61], "Spa\u{0308}ßchen", 2],
[[0xCC, 0x88], "Spa\u{0308}ßchen", 3],
[[0xE0, 0xA5, 0x8D], 'नमस्ते', 3],
]
);
}

public static function provideCodePointsAt(): array
{
return array_merge(
parent::provideCodePointsAt(),
[
[[0x61], "Spa\u{0308}ßchen", 2],
[[0x0308], "Spa\u{0308}ßchen", 3],
[[0x094D], 'नमस्ते', 3],
]
);
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/String/Tests/UnicodeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public static function provideChunk(): array
);
}

public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3, 0xA4], "Spa\u{0308}ßchen", 2],
[[0x61, 0xCC, 0x88], "Spa\u{0308}ßchen", 2, UnicodeString::NFD],
[[0xE0, 0xA4, 0xB8, 0xE0, 0xA5, 0x8D], 'नमस्ते', 2],
]
);
}

public static function provideCodePointsAt(): array
{
return array_merge(
parent::provideCodePointsAt(),
[
[[0xE4], "Spa\u{0308}ßchen", 2],
[[0x61, 0x0308], "Spa\u{0308}ßchen", 2, UnicodeString::NFD],
[[0x0938, 0x094D], 'नमस्ते', 2],
]
);
}

public static function provideLower(): array
{
return array_merge(
Expand Down