Skip to content
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
18 changes: 18 additions & 0 deletions wurst/util/StringUtils.wurst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package StringUtils
import NoWurst
import LinkedList
import ArrayList
import Texttag
import Interpolation
import TypeCasting
Expand Down Expand Up @@ -108,6 +109,23 @@ public function string.split(string splitBy) returns LinkedList<string>
str = second
return list

/** Splits into specialized contiguous storage, preserving empty and trailing fields.
An empty delimiter returns the original string as one field. The caller owns the list. */
public function string.splitToArray(string delimiter) returns ArrayList<string>
let result = new ArrayList<string>()
let offset = delimiter.length()
if this == null or offset == 0
result.add(this)
return result
var start = 0
var position = this.indexOf(delimiter, start)
while position >= 0
result.add(start == position ? "" : this.substring(start, position))
start = position + offset
position = this.indexOf(delimiter, start)
result.add(start == this.length() ? "" : this.substring(start, this.length()))
Comment thread
Frotty marked this conversation as resolved.
return result

/** Replaces consecutive repeats of the same string with a single instance
Turns "strstr" into "str" */
public function string.reduce(string reduced) returns string
Expand Down
27 changes: 26 additions & 1 deletion wurst/util/StringUtilsTests.wurst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ import ObjectIds
(list.size() == 1).assertTrue()
destroy list

@Test function splitToArrayPreservesEmptyFieldsAndMultiCharacterDelimiters()
let fields = "::alpha::::beta::".splitToArray("::")
fields.size().assertEquals(5)
fields.get(0).assertEquals("")
fields.get(1).assertEquals("alpha")
fields.get(2).assertEquals("")
fields.get(3).assertEquals("beta")
fields.get(4).assertEquals("")
destroy fields
let empty = "".splitToArray(",")
empty.size().assertEquals(1)
empty.get(0).assertEquals("")
destroy empty
let unchanged = "alpha".splitToArray("")
unchanged.size().assertEquals(1)
unchanged.get(0).assertEquals("alpha")
destroy unchanged

@Test function splitToArrayPreservesNullInput()
string input = null
let fields = input.splitToArray(",")
fields.size().assertEquals(1)
(fields.get(0) == null).assertTrue()
destroy fields

@Test function testJoin()
let list = new LinkedList<string>()
..add("this", "is", "a", "string")
Expand Down Expand Up @@ -74,4 +99,4 @@ import ObjectIds
"1-2".isNumber().assertFalse()

@Test function testReplaceChars()
"abca".replaceChars("ca", "d").assertEquals("dbdd")
"abca".replaceChars("ca", "d").assertEquals("dbdd")
Loading