Skip to content

Commit e703d6b

Browse files
committed
Make String.split/Slice.split more consistent
Different languages take different approaches for how string splitting should behave. On Discord Tomoki Aonuma (@uasi) pointed out some odd/inconsistent behavior of our String.split/Slice.split implementation compared to other languages. This commit changes the behavior so it's more consistent with Rust. That is: 1. We generate a slice for each separator, instead of skipping the last empty slice 2. An empty input produces a single empty slice Changelog: changed
1 parent 06b6cfa commit e703d6b

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

std/src/std/bytes.inko

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ type pub inline Slice[T: Bytes] {
377377
# If the separator isn't present in `self`, the returned iterator yields
378378
# `self` exactly once.
379379
#
380+
# If `self` is empty then a single empty `String` is yielded.
381+
#
380382
# # Examples
381383
#
382384
# ```inko
@@ -395,7 +397,7 @@ type pub inline Slice[T: Bytes] {
395397
Stream.new(fn move {
396398
match index_of(separator, starting_at: off) {
397399
case Some(i) -> Option.Some(slice(off := i + sep_len, i))
398-
case _ if off < len -> Option.Some(slice(off := len, len))
400+
case _ if off <= len -> Option.Some(slice(off := len + 1, len))
399401
case _ -> Option.None
400402
}
401403
})

std/src/std/net/http/cookie.inko

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ type pub Cookie {
417417
input: String,
418418
) -> Result[Array[Cookie], ParseError] {
419419
input.split(';').try_reduce([], fn (cookies, slice) {
420-
cookies.push(try Cookie.parse_name_value(slice))
420+
if slice.size > 0 { cookies.push(try Cookie.parse_name_value(slice)) }
421+
421422
Result.Ok(cookies)
422423
})
423424
}

std/test/std/test_string.inko

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ fn pub tests(t: mut Tests) {
189189
t.equal(split('foo//bar/baz', on: '/'), ['foo', '', 'bar', 'baz'])
190190
t.equal(split('foo/bar/baz', on: ''), ['foo/bar/baz'])
191191
t.equal(split('foo/bar', on: '///////////////'), ['foo/bar'])
192-
t.equal(split('foo/', on: '/'), ['foo'])
193-
t.equal(split('foo//', on: '/'), ['foo', ''])
194-
t.equal(split('foo///', on: '/'), ['foo', '', ''])
195-
t.equal(split('foo//', on: '//'), ['foo'])
192+
t.equal(split('foo/', on: '/'), ['foo', ''])
193+
t.equal(split('foo//', on: '/'), ['foo', '', ''])
194+
t.equal(split('foo///', on: '/'), ['foo', '', '', ''])
195+
t.equal(split('foo//', on: '//'), ['foo', ''])
196196
t.equal(split('foo///', on: '//'), ['foo', '/'])
197-
t.equal(split('', on: '/'), [])
197+
t.equal(split('/foo//', on: '/'), ['', 'foo', '', ''])
198+
t.equal(split('', on: '/'), [''])
198199
})
199200

200201
t.test('String.split_once', fn (t) {

0 commit comments

Comments
 (0)