From 2672253419813f6457366a66aaf93faea48f43f6 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 30 Mar 2017 20:53:20 +0200 Subject: it::split as frontend for preg_split() --- it.class | 14 ++++++++++++++ tests/it.t | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/it.class b/it.class index a96032f..1403013 100644 --- a/it.class +++ b/it.class @@ -454,6 +454,20 @@ static function grep($pattern, $array, $p = array()) return $result; } + +/** + * Returns string split by pattern (like preg_split but utf8 and case insensitive) + * @param $pattern Regex to match against, no delimiters + * @param $subject String to split + * @param $p['limit'], $p['no_empty'], $p['delim_capture'], $p['offset_capture'] See preg_split() + */ +static function split($pattern, $subject, $p = array()) +{ + $flags = ($p['no_empty'] ? PREG_SPLIT_NO_EMPTY : 0) | ($p['delim_capture'] ? PREG_SPLIT_DELIM_CAPTURE : 0) | ($p['offset_capture'] ? PREG_SPLIT_OFFSET_CAPTURE : 0); + + return preg_split(it::convertregex($pattern, $p), $subject, $p['limit'] ?: -1, $flags); +} + /** * Convert string or array to utf8 if it was not already utf-8 before. Also handles double encoding * @param $value String or array to convert diff --git a/tests/it.t b/tests/it.t index 0c02106..77bab72 100755 --- a/tests/it.t +++ b/tests/it.t @@ -442,3 +442,11 @@ is(it::map('2*$v', ['foo' => 1, 'bar' => 2], ['keys' => 'foo']), ['foo' => 2, 'b is(it::add_dir("foo/bar"), "foo/37/bar"); is(it::add_dir("baz/bar"), "baz/37/bar"); is(it::add_dir("bar"), "./37/bar"); + +is(it::split("b", "aba"), ["a", "a"]); +is(it::split("b", "aBa"), ["a", "a"]); +is(it::split("b", "abba"), ["a", "", "a"]); +is(it::split("b", "ababa", ['limit' => 2]), ["a", "aba"]); +is(it::split("b", "abbba", ['no_empty' => true]), ["a", "a"]); +is(it::split("(b)", "aba", ['delim_capture' => true]), ["a", "b", "a"]); +is(it::split("b", "aabaa", ['offset_capture' => true]), [["aa", 0], ["aa", 3]]); -- cgit v1.2.3