Skip to content

Commit 59d19f6

Browse files
Myaamoriline0
authored andcommitted
string: Add max_parts argument to split
1 parent 9d06e6e commit 59d19f6

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

Functional.moon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ _string = {
388388

389389
escRegExp: (str) -> str\gsub "([\\/%^%$%.|%?%*%+%(%)%[%]%{%}])", "\\%1"
390390

391-
split: (str, sep = " ", init = 1, plain = true) ->
391+
split: (str, sep = " ", init = 1, plain = true, limit = -1) ->
392392
first, last = str\find sep, init, plain
393393
-- fast return if there's nothing to split - saves one str.sub()
394-
return {str} unless first
394+
return {str}, 1 if not first or limit == 0
395395

396396
splits, s = {}, 1
397-
while first
397+
while first and s != limit + 1
398398
splits[s] = str\sub init, first - 1
399399
s += 1
400400
init = last + 1

Tests.moon

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,36 @@ DependencyControl.UnitTestSuite "l0.Functional", (functional, deps) ->
177177
"removeValues", "slice", "trimEnd", "trimBoth", "map", "mapCompact", "reduce", "pluck",
178178
"uniq", "uniqCallback", "listMetaType", "removeWhere"}
179179
}
180+
StringFunctions: {
181+
_description: "Test all functions for strings (string.*)"
182+
_setup: (ut) ->
183+
testData = {
184+
string: "This is a test."
185+
uniString: "仕方が無い"
186+
splitString: "a,b,,cde,%w+f,"
187+
}
188+
189+
return testData
190+
191+
split: (ut, strs) ->
192+
a, an = functional.string.split strs.splitString, ","
193+
b, bn = functional.string.split strs.splitString, ",,"
194+
c, cn = functional.string.split strs.splitString, ",", 5
195+
d, dn = functional.string.split strs.splitString, "%w+", 1, true
196+
e, en = functional.string.split strs.splitString, "%w+", 1, false
197+
f, fn = functional.string.split strs.splitString, ",", 1, true, 2
198+
g, gn = functional.string.split strs.splitString, ",", 1, true, 20
199+
h, hn = functional.string.split strs.splitString, "!"
200+
ut\assertEquals a, {"a", "b", "", "cde", "%w+f", ""}
201+
ut\assertEquals b, {"a,b", "cde,%w+f,"}
202+
ut\assertEquals c, {"", "cde", "%w+f", ""}
203+
ut\assertEquals d, {"a,b,,cde,", "f,"}
204+
ut\assertEquals e, {"", ",", ",,", ",%", "+", ","}
205+
ut\assertEquals f, {"a", "b", ",cde,%w+f,"}
206+
ut\assertEquals g, {"a", "b", "", "cde", "%w+f", ""}
207+
ut\assertEquals h, {strs.splitString}
208+
ut\assertEquals {an, bn, cn, dn, en, fn, gn, hn}, {6, 2, 4, 2, 6, 3, 6, 1}
209+
210+
_order: {"split"}
211+
}
180212
}

0 commit comments

Comments
 (0)