mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
More stuff for working with strings and bytes!
- "\x" escape sequence to put byte values in strings: "\x34" - String.byteAt(index) gets value of byte in string. - String.bytes returns a raw sequence of bytes for a string. - String.codePointAt(index) gets the code point at an offset as a raw number.
This commit is contained in:
38
test/core/string/byte_at.wren
Normal file
38
test/core/string/byte_at.wren
Normal file
@ -0,0 +1,38 @@
|
||||
// Bytes: 11111
|
||||
// 012345678901234
|
||||
// Chars: sø mé ஃ thî ng
|
||||
var s = "søméஃthîng"
|
||||
|
||||
IO.print(s.byteAt(0)) // expect: 115
|
||||
IO.print(s.byteAt(1)) // expect: 195
|
||||
IO.print(s.byteAt(2)) // expect: 184
|
||||
IO.print(s.byteAt(3)) // expect: 109
|
||||
IO.print(s.byteAt(4)) // expect: 195
|
||||
IO.print(s.byteAt(5)) // expect: 169
|
||||
IO.print(s.byteAt(6)) // expect: 224
|
||||
IO.print(s.byteAt(7)) // expect: 174
|
||||
IO.print(s.byteAt(8)) // expect: 131
|
||||
IO.print(s.byteAt(9)) // expect: 116
|
||||
IO.print(s.byteAt(10)) // expect: 104
|
||||
IO.print(s.byteAt(11)) // expect: 195
|
||||
IO.print(s.byteAt(12)) // expect: 174
|
||||
IO.print(s.byteAt(13)) // expect: 110
|
||||
IO.print(s.byteAt(14)) // expect: 103
|
||||
|
||||
IO.print(s.byteAt(-15)) // expect: 115
|
||||
IO.print(s.byteAt(-14)) // expect: 195
|
||||
IO.print(s.byteAt(-13)) // expect: 184
|
||||
IO.print(s.byteAt(-12)) // expect: 109
|
||||
IO.print(s.byteAt(-11)) // expect: 195
|
||||
IO.print(s.byteAt(-10)) // expect: 169
|
||||
IO.print(s.byteAt(-9)) // expect: 224
|
||||
IO.print(s.byteAt(-8)) // expect: 174
|
||||
IO.print(s.byteAt(-7)) // expect: 131
|
||||
IO.print(s.byteAt(-6)) // expect: 116
|
||||
IO.print(s.byteAt(-5)) // expect: 104
|
||||
IO.print(s.byteAt(-4)) // expect: 195
|
||||
IO.print(s.byteAt(-3)) // expect: 174
|
||||
IO.print(s.byteAt(-2)) // expect: 110
|
||||
IO.print(s.byteAt(-1)) // expect: 103
|
||||
|
||||
IO.print("\0".byteAt(0)) // expect: 0
|
||||
1
test/core/string/byte_at_not_int.wren
Normal file
1
test/core/string/byte_at_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".byteAt(12.34)) // expect runtime error: Index must be an integer.
|
||||
1
test/core/string/byte_at_not_num.wren
Normal file
1
test/core/string/byte_at_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".byteAt("not num")) // expect runtime error: Index must be a number.
|
||||
1
test/core/string/byte_at_too_large.wren
Normal file
1
test/core/string/byte_at_too_large.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".byteAt(6)) // expect runtime error: Index out of bounds.
|
||||
1
test/core/string/byte_at_too_small.wren
Normal file
1
test/core/string/byte_at_too_small.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".byteAt(-7)) // expect runtime error: Index out of bounds.
|
||||
6
test/core/string/bytes.wren
Normal file
6
test/core/string/bytes.wren
Normal file
@ -0,0 +1,6 @@
|
||||
// Bytes: 11111
|
||||
// 012345678901234
|
||||
// Chars: sø mé ஃ thî ng
|
||||
var s = "søméஃthîng"
|
||||
|
||||
IO.print(s.bytes is StringByteSequence) // expect: true
|
||||
38
test/core/string/code_point_at.wren
Normal file
38
test/core/string/code_point_at.wren
Normal file
@ -0,0 +1,38 @@
|
||||
// Bytes: 11111
|
||||
// 012345678901234
|
||||
// Chars: sø mé ஃ thî ng
|
||||
var s = "søméஃthîng"
|
||||
|
||||
IO.print(s.codePointAt(0)) // expect: 115
|
||||
IO.print(s.codePointAt(1)) // expect: 248
|
||||
IO.print(s.codePointAt(2)) // expect: -1
|
||||
IO.print(s.codePointAt(3)) // expect: 109
|
||||
IO.print(s.codePointAt(4)) // expect: 233
|
||||
IO.print(s.codePointAt(5)) // expect: -1
|
||||
IO.print(s.codePointAt(6)) // expect: 2947
|
||||
IO.print(s.codePointAt(7)) // expect: -1
|
||||
IO.print(s.codePointAt(8)) // expect: -1
|
||||
IO.print(s.codePointAt(9)) // expect: 116
|
||||
IO.print(s.codePointAt(10)) // expect: 104
|
||||
IO.print(s.codePointAt(11)) // expect: 238
|
||||
IO.print(s.codePointAt(12)) // expect: -1
|
||||
IO.print(s.codePointAt(13)) // expect: 110
|
||||
IO.print(s.codePointAt(14)) // expect: 103
|
||||
|
||||
IO.print(s.codePointAt(-15)) // expect: 115
|
||||
IO.print(s.codePointAt(-14)) // expect: 248
|
||||
IO.print(s.codePointAt(-13)) // expect: -1
|
||||
IO.print(s.codePointAt(-12)) // expect: 109
|
||||
IO.print(s.codePointAt(-11)) // expect: 233
|
||||
IO.print(s.codePointAt(-10)) // expect: -1
|
||||
IO.print(s.codePointAt(-9)) // expect: 2947
|
||||
IO.print(s.codePointAt(-8)) // expect: -1
|
||||
IO.print(s.codePointAt(-7)) // expect: -1
|
||||
IO.print(s.codePointAt(-6)) // expect: 116
|
||||
IO.print(s.codePointAt(-5)) // expect: 104
|
||||
IO.print(s.codePointAt(-4)) // expect: 238
|
||||
IO.print(s.codePointAt(-3)) // expect: -1
|
||||
IO.print(s.codePointAt(-2)) // expect: 110
|
||||
IO.print(s.codePointAt(-1)) // expect: 103
|
||||
|
||||
IO.print("\0".codePointAt(0)) // expect: 0
|
||||
3
test/core/string/code_point_at_incomplete.wren
Normal file
3
test/core/string/code_point_at_incomplete.wren
Normal file
@ -0,0 +1,3 @@
|
||||
// The first two bytes of a three-octet sequence.
|
||||
var s = "\xe0\xae"
|
||||
IO.print(s.codePointAt(0)) // expect: -1
|
||||
1
test/core/string/code_point_at_not_int.wren
Normal file
1
test/core/string/code_point_at_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".codePointAt(12.34)) // expect runtime error: Index must be an integer.
|
||||
1
test/core/string/code_point_at_not_num.wren
Normal file
1
test/core/string/code_point_at_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".codePointAt("not num")) // expect runtime error: Index must be a number.
|
||||
1
test/core/string/code_point_at_too_large.wren
Normal file
1
test/core/string/code_point_at_too_large.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".codePointAt(6)) // expect runtime error: Index out of bounds.
|
||||
1
test/core/string/code_point_at_too_small.wren
Normal file
1
test/core/string/code_point_at_too_small.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print("string".codePointAt(-7)) // expect runtime error: Index out of bounds.
|
||||
21
test/core/string_byte_sequence/iterate.wren
Normal file
21
test/core/string_byte_sequence/iterate.wren
Normal file
@ -0,0 +1,21 @@
|
||||
// Bytes:
|
||||
// 012345678
|
||||
// Chars: sø mé ஃ
|
||||
var bytes = "søméஃ".bytes
|
||||
|
||||
IO.print(bytes.iterate(null)) // expect: 0
|
||||
IO.print("".bytes.iterate(null)) // expect: false
|
||||
|
||||
IO.print(bytes.iterate(0)) // expect: 1
|
||||
IO.print(bytes.iterate(1)) // expect: 2
|
||||
IO.print(bytes.iterate(2)) // expect: 3
|
||||
IO.print(bytes.iterate(3)) // expect: 4
|
||||
IO.print(bytes.iterate(4)) // expect: 5
|
||||
IO.print(bytes.iterate(5)) // expect: 6
|
||||
IO.print(bytes.iterate(6)) // expect: 7
|
||||
IO.print(bytes.iterate(7)) // expect: 8
|
||||
IO.print(bytes.iterate(8)) // expect: false
|
||||
|
||||
// Out of bounds.
|
||||
IO.print(bytes.iterate(123)) // expect: false
|
||||
IO.print(bytes.iterate(-1)) // expect: false
|
||||
1
test/core/string_byte_sequence/iterate_not_int.wren
Normal file
1
test/core/string_byte_sequence/iterate_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
"str".bytes.iterate(12.34) // expect runtime error: Iterator must be an integer.
|
||||
1
test/core/string_byte_sequence/iterate_wrong_type.wren
Normal file
1
test/core/string_byte_sequence/iterate_wrong_type.wren
Normal file
@ -0,0 +1 @@
|
||||
"str".bytes.iterate("not num") // expect runtime error: Iterator must be a number.
|
||||
24
test/core/string_byte_sequence/iterator_value.wren
Normal file
24
test/core/string_byte_sequence/iterator_value.wren
Normal file
@ -0,0 +1,24 @@
|
||||
// Bytes:
|
||||
// 012345678
|
||||
// Chars: sø mé ஃ
|
||||
var bytes = "søméஃ".bytes
|
||||
|
||||
IO.print(bytes.iteratorValue(0)) // expect: 115
|
||||
IO.print(bytes.iteratorValue(1)) // expect: 195
|
||||
IO.print(bytes.iteratorValue(2)) // expect: 184
|
||||
IO.print(bytes.iteratorValue(3)) // expect: 109
|
||||
IO.print(bytes.iteratorValue(4)) // expect: 195
|
||||
IO.print(bytes.iteratorValue(5)) // expect: 169
|
||||
IO.print(bytes.iteratorValue(6)) // expect: 224
|
||||
IO.print(bytes.iteratorValue(7)) // expect: 174
|
||||
IO.print(bytes.iteratorValue(8)) // expect: 131
|
||||
|
||||
IO.print(bytes.iteratorValue(-9)) // expect: 115
|
||||
IO.print(bytes.iteratorValue(-8)) // expect: 195
|
||||
IO.print(bytes.iteratorValue(-7)) // expect: 184
|
||||
IO.print(bytes.iteratorValue(-6)) // expect: 109
|
||||
IO.print(bytes.iteratorValue(-5)) // expect: 195
|
||||
IO.print(bytes.iteratorValue(-4)) // expect: 169
|
||||
IO.print(bytes.iteratorValue(-3)) // expect: 224
|
||||
IO.print(bytes.iteratorValue(-2)) // expect: 174
|
||||
IO.print(bytes.iteratorValue(-1)) // expect: 131
|
||||
@ -0,0 +1 @@
|
||||
"abcd".bytes.iteratorValue(12.34) // expect runtime error: Index must be an integer.
|
||||
@ -0,0 +1 @@
|
||||
"abcd".bytes.iteratorValue("not num") // expect runtime error: Index must be a number.
|
||||
@ -0,0 +1 @@
|
||||
"abcd".bytes.iteratorValue(4) // expect runtime error: Index out of bounds.
|
||||
@ -0,0 +1 @@
|
||||
"abcd".bytes.iteratorValue(-5) // expect runtime error: Index out of bounds.
|
||||
24
test/core/string_byte_sequence/subscript.wren
Normal file
24
test/core/string_byte_sequence/subscript.wren
Normal file
@ -0,0 +1,24 @@
|
||||
// Bytes:
|
||||
// 012345678
|
||||
// Chars: sø mé ஃ
|
||||
var bytes = "søméஃ".bytes
|
||||
|
||||
IO.print(bytes[0]) // expect: 115
|
||||
IO.print(bytes[1]) // expect: 195
|
||||
IO.print(bytes[2]) // expect: 184
|
||||
IO.print(bytes[3]) // expect: 109
|
||||
IO.print(bytes[4]) // expect: 195
|
||||
IO.print(bytes[5]) // expect: 169
|
||||
IO.print(bytes[6]) // expect: 224
|
||||
IO.print(bytes[7]) // expect: 174
|
||||
IO.print(bytes[8]) // expect: 131
|
||||
|
||||
IO.print(bytes[-9]) // expect: 115
|
||||
IO.print(bytes[-8]) // expect: 195
|
||||
IO.print(bytes[-7]) // expect: 184
|
||||
IO.print(bytes[-6]) // expect: 109
|
||||
IO.print(bytes[-5]) // expect: 195
|
||||
IO.print(bytes[-4]) // expect: 169
|
||||
IO.print(bytes[-3]) // expect: 224
|
||||
IO.print(bytes[-2]) // expect: 174
|
||||
IO.print(bytes[-1]) // expect: 131
|
||||
1
test/core/string_byte_sequence/subscript_not_int.wren
Normal file
1
test/core/string_byte_sequence/subscript_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
"abcd".bytes[12.34] // expect runtime error: Index must be an integer.
|
||||
1
test/core/string_byte_sequence/subscript_not_num.wren
Normal file
1
test/core/string_byte_sequence/subscript_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
"abcd".bytes["not num"] // expect runtime error: Index must be a number.
|
||||
1
test/core/string_byte_sequence/subscript_too_large.wren
Normal file
1
test/core/string_byte_sequence/subscript_too_large.wren
Normal file
@ -0,0 +1 @@
|
||||
"abcd".bytes[4] // expect runtime error: Index out of bounds.
|
||||
1
test/core/string_byte_sequence/subscript_too_small.wren
Normal file
1
test/core/string_byte_sequence/subscript_too_small.wren
Normal file
@ -0,0 +1 @@
|
||||
"abcd".bytes[-5] // expect runtime error: Index out of bounds.
|
||||
12
test/language/string/byte_escapes.wren
Normal file
12
test/language/string/byte_escapes.wren
Normal file
@ -0,0 +1,12 @@
|
||||
var s = "\x00\x12\x34\x56\x78\xab\xCD\xfFf"
|
||||
|
||||
IO.print(s.byteAt(0)) // expect: 0
|
||||
IO.print(s.byteAt(1)) // expect: 18
|
||||
IO.print(s.byteAt(2)) // expect: 52
|
||||
IO.print(s.byteAt(3)) // expect: 86
|
||||
IO.print(s.byteAt(4)) // expect: 120
|
||||
IO.print(s.byteAt(5)) // expect: 171
|
||||
IO.print(s.byteAt(6)) // expect: 205
|
||||
IO.print(s.byteAt(7)) // expect: 255
|
||||
// "f".
|
||||
IO.print(s.byteAt(8)) // expect: 102
|
||||
2
test/language/string/incomplete_byte_escape.wren
Normal file
2
test/language/string/incomplete_byte_escape.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// expect error line 2
|
||||
"\x0"
|
||||
2
test/language/string/incomplete_byte_escape_at_eof.wren
Normal file
2
test/language/string/incomplete_byte_escape_at_eof.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// expect error line 2
|
||||
"\x0
|
||||
2
test/language/string/invalid_byte_escape.wren
Normal file
2
test/language/string/invalid_byte_escape.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// expect error line 2
|
||||
"\x0!"
|
||||
Reference in New Issue
Block a user