krakjoe / ustring

UnicodeString for PHP7
Other
64 stars 7 forks source link

No split() method #10

Closed hikari-no-yume closed 10 years ago

hikari-no-yume commented 10 years ago

C'mon, strings gotta have one. Either split or explode, I'd go with the former as it's a more common name in other languages, and it's the verb the manual uses to describe what explode does.

I'd suggest this signature (pseudo-code):

UString::split(UString $delimeter, int $limit = NULL): Array<UString>

The optional $limit parameter is a number. If specified, then the string is only split $limit times. This is a really useful feature.

Examples:

(new UString("1,2,3,4"))->split(new UString(","));
// => ["1", "2", "3", "4"]

(new UString("1,2,3,4"))->split(new UString(","), 1);
// => ["1", "2,3,4"]

(new UString("1,2,3,4"))->split(new UString(","), 2);
// => ["1", "2", "3,4"]

(new UString(",1,2,,3,4,"))->split(new UString(","));
// => ["", "1", "2", "", "3", "4", ""]

One way to implement it (in Game Maker Language, it's C-like but strings indexed from 1): https://github.com/Medo42/Gang-Garrison-2/blob/master/Source/gg2/Scripts/split.gml

datibbaw commented 10 years ago

Fix it, fix it, fix it! :)

On 25 Aug, 2014, at 11:55 pm, Andrea Faulds notifications@github.com wrote:

C'mon, strings gotta have one. Either split or explode, I'd go with the former as it's a more common name in other languages, and it's the verb the manual uses to describe what explode does.

I'd suggest this signature (pseudo-code):

UString::split(UString $delimeter, ?int $limit = NULL): Array The optional $limit parameter is a number. If specified, then the string is only split $limit times. This is really useful.

Examples:

(new UString("1,2,3,4"))->split(new UString(",")); // => ["1", "2", "3", "4"]

(new UString("1,2,3,4"))->split(new UString(","), 1); // => ["1", "2,3,4"]

(new UString("1,2,3,4"))->split(new UString(","), 2); // => ["1", "2", "3,4"]

(new UString(",1,2,,3,4,"))->split(new UString(",")); // => ["", "1", "2", "", "3", "4", ""] — Reply to this email directly or view it on GitHub.

krakjoe commented 10 years ago

+1

hikari-no-yume commented 10 years ago

Done: https://github.com/krakjoe/ustring/commit/3b72ddddd4327ccb522ef065f71255dad320e7b8

krakjoe commented 10 years ago

Nicely done, thanks :)

datibbaw commented 10 years ago

What was the ::pad() bug?

On 26 Aug, 2014, at 3:14 am, Andrea Faulds notifications@github.com wrote:

Done: 3b72ddd

— Reply to this email directly or view it on GitHub.

hikari-no-yume commented 10 years ago

@datibbaw When a string was passed, you used the size in bytes as the length. I fixed it to take the length of the resulting UnicodeString.