Parse UTF-8 into String (#297)

* Add some functions related to Bytes and others

* Add some byte functions and tests

* Add more bytes funcs and decode for utf-8 - needs to test

* Update some names in tests for bytes

* Update some test

* Update kind version on Playground

* Add some bytes funcs

* WIP with Bytes.to_string

* WIP Bytes.parse_string.aux grrr

* Can decode Bytes.test.parse_string.2

* Can parse some strings

* Most part of string parsing from UTF-8

* Clean some code and update tests in Bytes.test.all

* fix from victor version

* Remove Scheme target, add comment about WIP

Co-authored-by: caotic123 <camposferreiratiago@gmail.com>
This commit is contained in:
MaisaMilena 2021-10-04 23:15:58 -03:00 committed by GitHub
parent 0111bb8d2c
commit ec49dafd9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 590 additions and 42 deletions

View File

@ -114,7 +114,7 @@ App.Playground.when: App.When<App.Playground.State>
App.set_local!(stt)
else
IO {
get checked = IO.request("http://18.222.191.174:3030/api/check_term?code=" | state.code)
get checked = IO.request("http://18.222.191.174:3030/api/check_term?code='" | state.code | "'")
let stt = App.Playground.set_output(checked, state)
let stt = App.Playground.set_window(window, stt)
App.set_local!(stt)

View File

@ -143,7 +143,7 @@ App.Playground.comp.output_area(output: String, device: Device): DOM
Map.union<String>(style, {
"height": "100% - 35px"
"margin-top": "35px"}),
[ DOM.node("div", {}, {}, [DOM.text("kind-lang@1.0.51")])
[ DOM.node("div", {}, {}, [DOM.text("kind-lang@1.0.89")])
DOM.node("pre", {}, {"margin-top": "10px"}, [DOM.text(output)])]
)

View File

@ -1,7 +1,7 @@
Buffer32.from(str: String): Buffer32
let size = String.length(str) //((String.byte_length(str) - 1) / 4) + 1
// let buf = Buffer32.alloc(Nat.bitlen(size) + 1)
// FIXME: this size is wrong. It much better than it need to be.
// FIXME: this size is wrong. It's bigger than it needs to be.
let buf = Buffer32.alloc(size)
// log("size: " | Nat.show(size))
Buffer32.from.go(str, buf, 0)

View File

@ -6,4 +6,6 @@ Buffer8.to_string(buf: Buffer8): String
let bits = U8.to_bits(Buffer8.get(Nat.to_u32(i), buf)) // group of 4 bytes
let hex = String.reverse(Bits.hex.encode(bits))
str | " " | hex
str
str

4
base/Bytes.kind Normal file
View File

@ -0,0 +1,4 @@
// A Bytes
// valid byte range (i.e. between 0 and 255 inclusive)
Bytes: Type
String

4
base/Bytes/arrayify.kind Normal file
View File

@ -0,0 +1,4 @@
// Convert a hexstring to a Uint8Array
Bytes.arrayify(value: String): Buffer8
let {_,buf} = Buffer8.from_hex(String.to_hex(value))
buf

11
base/Bytes/at.kind Normal file
View File

@ -0,0 +1,11 @@
// Return 2 characters from "bytes", if available
Bytes.at(bytes: String): Maybe(String)
case bytes {
nil: Maybe.none!
cons:
case bytes.tail {
nil: Maybe.none!
cons:
Maybe.some!(Char.to_string(bytes.head) | Char.to_string(bytes.tail.head))
}
}

2
base/Bytes/drop.kind Normal file
View File

@ -0,0 +1,2 @@
Bytes.drop(n: Nat, b: Bytes): Bytes
String.drop(n, b)

6
base/Bytes/from_nat.kind Normal file
View File

@ -0,0 +1,6 @@
Bytes.from_nat(num: Nat): String
let hex = Nat.to_string_base(16, num)
if Nat.eql((String.length(hex) % 2), 0)
then "0x" | hex
else "0x0" | hex

View File

@ -0,0 +1,2 @@
Bytes.from_string(s: String): Bytes
"0x"|String.to_hex(s)

2
base/Bytes/length.kind Normal file
View File

@ -0,0 +1,2 @@
Bytes.length(b: Bytes): Nat
(String.length(b) - 2) / 2

7
base/Bytes/pad.kind Normal file
View File

@ -0,0 +1,7 @@
// Bytes.pad(len: Nat, hex: String): String
// if Nat.eql(String.length(hex), (len*2)+2)
// then hex
// else Bytes.pad(len, "0x" | "0" | String.slice(0, 2, hex))
Bytes.pad_left(len: Nat, hex: String): String
"0x" | String.pad_left(len, String.to_char("0"), hex)

View File

@ -0,0 +1,2 @@
Bytes.pad_right(len: Nat, hex: String): String
String.pad_right(len, String.to_char("0"), hex)

View File

@ -0,0 +1,93 @@
// 28 Sep 21
// Author: Maisa Milena
// FIXME:
// - parse characters using 3 and 4 bytes
// - deal with parse error
// Hint: check the test cases in Bytes/test/parse_string.kind
// and add more tests
// IMPORTANT: do not use these functions yet. Still WIP
// Decode a UTF8 data into a String
Bytes.parse_string(b: Bytes): String
Bytes.parse_string.aux(b, "")
Bytes.parse_string.take_bytes_pair(b: Bytes): Pair(Bytes, Maybe(Bytes))
let fst = Bytes.take(2, b)
let snd = String.drop(2, Bytes.take(4, b))
if String.is_empty(snd)
then {fst, Maybe.none!}
else {fst, Maybe.some!(snd)}
Bytes.parse_string.aux(b: Bytes, s: String): String
// Auxiliary
let concat_s = (a: String) s | a
let b_tail = String.drop(2, b)
let {fst, snd} = Bytes.parse_string.take_bytes_pair(b)
case snd {
none: s
some:
let snd = snd.value
let snd = Bytes.to_nat(snd)
let fst = Bytes.to_nat(fst)
let fst = Nat.to_u16(fst)
let snd = Nat.to_u16(snd)
let control_num = 999#16
let s_temp =
if U16.gtn(snd, 127) then
case Bytes.parse_string.gtn_one_byte(fst, snd) as res {
none: control_num // got parsing error or help parsing symbols. Confusing, I know
some: res.value
}
else
snd
if U16.ltn(s_temp, 0xffff) then
// log("c ltn 0xffff: " | U16.show(s_temp))
let parsed =
if U16.eql(s_temp, control_num)
then "" // Couldn't parse byte, but may be not and error (like for some symbols)
else Char.to_string(s_temp)
// log(" parsed value: " | parsed)
Bytes.parse_string.aux(b_tail, concat_s(parsed)) // String.fromCharCode(c)
else if U16.lte(s_temp, 0x10ffff) then// c <= 0x10ffff
let aux = U16.sub(s_temp, 0x10000)
let a = Char.to_string(U16.or(U16.shr(s_temp, 10), 0xd800)) // String.fromCharCode(c >> 10 | 0xd800)
let b = Char.to_string(U16.or(U16.and(s_temp, 0x3FF), 0xdc00)) // String.fromCharCode(c & 0x3FF | 0xdc00)
let parsed = a | b
// log("c ltn 0x10ffff: " | parsed)
Bytes.parse_string.aux(b_tail, concat_s(parsed))
else ""
}
// Auxiliar to parse chunks bigger than 1 byte
Bytes.parse_string.gtn_one_byte(c: U16, fst: U16): Maybe(U16)
let two_bytes = Bool.and(U16.gtn(c, 191), U16.ltn(c, 224)) // (c > 191 && c < 224)
let three_bytes = Bool.and(U16.gtn(c, 223), U16.ltn(c, 240)) // (c > 223 && c < 240)
let four_bytes = Bool.and(U16.gtn(c, 239), U16.ltn(c, 248)) // (c > 239 && c < 248)
if two_bytes then
let l = U16.shl(U16.and(c, 31), 6) // (c & 31) << 6
let r = U16.and(fst, 63) // bytes[i++] & 63
let res = U16.or(l, r)
Maybe.some!(res) // l | r
else if three_bytes then
let l = U16.shl(U16.and(c, 15), 12) // (c & 15) << 12
let m = U16.shl(U16.and(fst, 63), 6) // (bytes[i++] & 63) << 6
let r = U16.and(fst, 63) // bytes[i++] & 63
let res = U16.or(l, U16.or(m, r))
Maybe.some!(res) // l | m | r;
else if four_bytes then
let l = U16.shl(U16.and(c, 7), 18) // (c & 7) << 18
let m0 = U16.shl(U16.and(fst, 63), 12) // (bytes[i++] & 63) << 12
let m1 = U16.shl(U16.and(fst, 63), 6) // (bytes[i++] & 63) << 6
let r = U16.and(fst, 63) // bytes[i++] & 63
let res = U16.or(U16.or(l, m0), U16.or(m1, r))
Maybe.some!(res) // l | m0 | m1 | r
else Maybe.none! // UTF-8 decode: unknown multibyte or auxiliary to parse symbols
// Reference:
// - https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330
// -

7
base/Bytes/reverse.kind Normal file
View File

@ -0,0 +1,7 @@
// https://github.com/MaiaVictor/eth-lib/blob/da0971f5b09964d9c8449975fa87933f0c9fef35/lib/bytes.js#L19
Bytes.reverse(hex: String): String
let rev = "0x"
let len = ((String.length(hex) - 2) / 2)
for i from 0 to len with rev:
String.concat(rev, String.slice((len-i)*2, ((len-i)+1)*2, hex))
rev

2
base/Bytes/slice.kind Normal file
View File

@ -0,0 +1,2 @@
Bytes.slice(i: Nat, j: Nat, bs: Bytes): Bytes
"0x" | String.slice((i*2)+2, ((j*2)+2), bs)

3
base/Bytes/take.kind Normal file
View File

@ -0,0 +1,3 @@
// Take the first 'n' characters from 'b'
Bytes.take(n: Nat, b: Bytes): Bytes
String.take(n, b)

18
base/Bytes/test/all.kind Normal file
View File

@ -0,0 +1,18 @@
Bytes.test.all: _
TestSuite.many("Bytes",
[
Bytes.test.from_nat
Bytes.test.length
Bytes.test.slice
Bytes.test.to_nat
Bytes.test.from_string
Bytes.test.parse_string
]
)
// Print all tests
Bytes.test.all.show: _
log(TestSuite.show(Bytes.test.all, 0))
0

View File

@ -0,0 +1,29 @@
Bytes.test.from_nat.0: TestSuite
let test = Bytes.from_nat(0);
let got = test
let exp = "0x00"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_nat.1: TestSuite
let test = Bytes.from_nat(100);
let got = test
let exp = "0x64"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_nat.2: TestSuite
let test = Bytes.from_nat(50);
let got = test
let exp = "0x32"
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_nat: TestSuite
TestSuite.many("from_nat", [
Bytes.test.from_nat.0
Bytes.test.from_nat.1
Bytes.test.from_nat.2
])

View File

@ -0,0 +1,53 @@
Bytes.test.from_string.0: TestSuite
let test = "some_random_string"
let got = Bytes.from_string(test)
let exp = "0x736f6d655f72616e646f6d5f737472696e67"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string.1: TestSuite
let test = ""
let got = Bytes.from_string(test)
let exp = "0x"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string.2: TestSuite
let test = "¢"
let got = Bytes.from_string(test)
let exp = "0xc2a2"
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string.3: TestSuite
let test = "ह€"
let got = Bytes.from_string(test)
let exp = "0xe0a4b9e282ac"
let name = "3"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string.4: TestSuite
let test = "𐍈"
let got = Bytes.from_string(test)
let exp = "0xf0908d88"
let name = "4"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string.5: TestSuite
let test = "0123456789"
let got = Bytes.from_string(test)
let exp = "0x30313233343536373839"
let name = "5"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.from_string: TestSuite
TestSuite.many("from_string", [
Bytes.test.from_string.0
Bytes.test.from_string.1
Bytes.test.from_string.2
Bytes.test.from_string.3
Bytes.test.from_string.4
Bytes.test.from_string.5
])

View File

@ -0,0 +1,44 @@
Bytes.test.length.0: TestSuite
let test = "0x883bb7da0342d870"
let got = Nat.show(Bytes.length(test))
let exp = "8"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.length.1: TestSuite
let test = "0x"
let got = Nat.show(Bytes.length(test))
let exp = "0"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.length.2: TestSuite
let test = "0xbeeca47320461d05ba928962339e46cef56a2f6dd11d9eea0d1fc0c4f2c762f25512959f243d108081c6a665e9e12c90f27db5741612787114f96f85c8ee5f0b0d30f1d8be4329705ca1dbaf500fd525542e02a11381f85717a1599031cb6898e11c6485823ea228b491440c404687696140ec70735a773a101f824aa25039ad2c15759a4e1ba0a546eec1ed12a5816aa7caa1abd7830837b9bdf46c86a7eec9b1424e6ef0329ebfec45317397a2021941d2a5825df14c32a729eb67fb5f39cf3fee8a86e7a5463ab347159cd3af7d3a4fd7eb4f5a643be150b6549e714f5e4f4f7fc53e817bbbe0b2b62a3ad0edbd924040cd796c2ab9a9bde214af841a8de4e4b976985fcff30605764e523edadc68130fdd97fcc93908ea3e323406574ff7fcf6c198e032417b7f0d5de4e8b4185ce00d8a16fe1039bbfbeecd2e67bbc88086ee96a849ee11f27ee17838be9f8ff3b1057050a862fc9d1f126e4f3507611f1800eca8f7502ee5083473d07a4729370080397656bab631788418f775e1de1f17653f5efe5945482ec1b7e50ca8990fd7284795f6d9ef54676a3bd0e814b742b064a9535f6068fd66944a68526962b0d472c74f01d0c02256318e82bbe18346a7009fdab34b1e4fc198ae70ba000d86a221f3508fbc22d286d1b277c23274bc065b37a51de314a0c6b5e78ca594f0336d83b8bbafcf27824bdee3e017c7ff7f03499d84dca28b4a11937e11f17a102683d6bd4e40c877681bf9f4e18fabbb6b816d392f07e5dc06e8375c112560abe34c093052748a06119a3bb14ee4ce765d787c02b83897cb47bf84fcc818078fcbf8c282ca3cc8a081f5264d9a31e226d518e43eaade939cb111ec5f3e3f84cdca5d8586ee1cfd96710f3c665ee77d399d38b504626f33b2384e8f50d86e8a1ea262adaa5f10d0d4a1f676ae4dfef0c72ddbb868f7c99634e6305fb93744a1b8814b6b7ac3bd988ccc446017b11028241e1b167a0fc7ae792ee158cf137f91379418be714522200aa2e6623de8bd6f26763c99a29ce4a27d2ae7699ca16f8b61f80117b89d9e536dc9bdf49e1526ffe5f8075c3f7a2949d3a6b6f2a276dd15f80236e847fc61307dc8a1947a2d965f5d3516d579b0a38d2c5a28656a069317b40939553d7a4df44561a18312e31c64b42b8449060de8daedbfd16288316cc11b7aaf921ca4b7fd9662ef35bc927338b6957665233cafed398f6a23fae50ec076ddf90fb83a7bf1e875447dc2ad0e42282ff253ef5ce9ff6fba07bad731c2e35ef9a8a61a896e1f79cc31e48cb013eb77919373a5fbed9533a50cdb0fa4fa131d3b3dc34c870cfeab5a260a7af57eb97a4b331f08e789906143107e61e71d847f343516d674c936c942e65f6f5edb9f1b8e2f0818f355e4c8b9"
let got = Nat.show(Bytes.length(test))
let exp = "1000"
let name = "2"
TestSuite.show.log_string(name, "long string", exp, got)
Bytes.test.length.3: TestSuite
let test = "0x2ebd18326908ff"
let got = Nat.show(Bytes.length(test))
let exp = "7"
let name = "3"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.length.4: TestSuite
let test = "0x0000000001"
let got = Nat.show(Bytes.length(test))
let exp = "5"
let name = "4"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.length: TestSuite
TestSuite.many("length", [
Bytes.test.length.0
Bytes.test.length.1
Bytes.test.length.2
Bytes.test.length.3
Bytes.test.length.4
])

View File

@ -0,0 +1,64 @@
Bytes.test.parse_string.0: TestSuite
let test = "0x"
let got = Bytes.parse_string(test)
let exp = ""
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.parse_string.1: TestSuite
let test = "0x30313233343536373839"
let got = Bytes.parse_string(test)
let exp = "0123456789"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
// 2 bytes
Bytes.test.parse_string.2: TestSuite
let test = "0xc2a2"
let got = Bytes.parse_string(test)
let exp = "¢"
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
// 3 bytes
Bytes.test.parse_string.3: TestSuite
let test = "0xe282ac"
let got = Bytes.parse_string(test)
let exp = "€"
let name = "3"
TestSuite.show.log_string(name, test, exp, got)
// 4 bytes
Bytes.test.parse_string.4: TestSuite
let test = "0xf0908d88"
let got = Bytes.parse_string(test)
let exp = "𐍈"
let name = "4"
TestSuite.show.log_string(name, test, exp, got)
// Unable to parse
Bytes.test.parse_string.5: TestSuite
let test = "0x708e08"
let got = Bytes.parse_string(test)
let exp = ""
let name = "5"
TestSuite.show.log_string(name, test, exp, got)
// String
Bytes.test.parse_string.6: TestSuite
let test = "0x736f6d655f72616e646f6d5f737472696e67"
let got = Bytes.parse_string(test)
let exp = "some_random_string"
let name = "6"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.parse_string: TestSuite
TestSuite.many("parse_string", [
Bytes.test.parse_string.0
Bytes.test.parse_string.1
Bytes.test.parse_string.2
Bytes.test.parse_string.3
Bytes.test.parse_string.4
Bytes.test.parse_string.5
Bytes.test.parse_string.6
])

View File

@ -0,0 +1,43 @@
Bytes.test.slice.0: TestSuite
let test = "0x4cf5fc74dff02752"
let got = Bytes.slice(0, 2, test)
let exp = "0x4cf5"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.slice.1: TestSuite
let test = "0x4cf5fc74dff02752"
let got = Bytes.slice(0, 0, test)
let exp = "0x"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.slice.2: TestSuite
let test = "0x4cf5fc74dff02752"
let got = Bytes.slice(2, 0, test)
let exp = "0x"
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.slice.3: TestSuite
let test = "0x"
let got = Bytes.slice(0, 2, test)
let exp = "0x"
let name = "3"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.slice.4: TestSuite
let test = "0x2384c637e6c83e"
let got = Bytes.slice(6, 9, test)
let exp = "0x3e"
let name = "4"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.slice: TestSuite
TestSuite.many("slice", [
Bytes.test.slice.0
Bytes.test.slice.1
Bytes.test.slice.2
Bytes.test.slice.3
Bytes.test.slice.4
])

View File

@ -0,0 +1,29 @@
Bytes.test.to_nat.0: TestSuite
let test = "30"
let got = Nat.show(Bytes.to_nat(test))
let exp = "48"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.to_nat.1: TestSuite
let test = "5f"
let got = Nat.show(Bytes.to_nat(test))
let exp = "101"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.to_nat.2: TestSuite
let test = ""
let got = Nat.show(Bytes.to_nat(test))
let exp = ""
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
Bytes.test.to_nat: TestSuite
TestSuite.many("to_nat", [
Bytes.test.to_nat.0
Bytes.test.to_nat.1
Bytes.test.to_nat.2
// Bytes.test.to_nat.3
// Bytes.test.to_nat.4
])

3
base/Bytes/to_nat.kind Normal file
View File

@ -0,0 +1,3 @@
Bytes.to_nat(b: Bytes): Nat
Nat.hex.decode(String.reverse(b))

2
base/BytesLike.kind Normal file
View File

@ -0,0 +1,2 @@
BytesLike: Type
Either<Bytes, String>

View File

@ -1,2 +1,10 @@
Nat.pow(x : Nat, y : Nat) : Nat
Nat.pow(x, y)
// x ^ y
Nat.pow(x: Nat, y: Nat): Nat
Nat.pow.aux(x, y, 1)
Nat.pow.aux(x: Nat, y: Nat, aux: Nat): Nat
case y {
zero: x
succ: Nat.pow.aux(x, y.pred, Nat.mul(aux, 2))
}

View File

@ -67,3 +67,18 @@ String.hex.encode.utf8(str: String): String
}
// TODO: create String.hex.decode.utf8
String.hex.decode.utf8(hex: String): String
//U16.from_hex.utf8(hex)
let rmv_start = String.drop(4, hex) // remove "0x" + first byte
Bytes.to_string.aux(rmv_start)
String.hex.decode.utf8.aux(hex: String): U16
log("b: "| hex)
let c = Bytes.take(2, hex)
String.to_u16(c)
String.hex.test: _
log("decode")
// String.hex.decode.utf8("0x3233343536373839")
String.hex.decode.utf8("0xa2")

View File

@ -2,7 +2,8 @@ String.test.all: TestSuite
TestSuite.many("String",
[
String.test.to_hex,
String.test.is_nat
String.test.is_nat,
String.test.to_u16
])
// Show all tests for String

View File

@ -9,39 +9,39 @@ String.test.is_nat.0: TestSuite
let got = Bool.show(String.is_nat(test))
let exp = "Bool.true"
let name = "0"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.is_nat.1: TestSuite
let test = "-987"
let got = Bool.show(String.is_nat(test))
let exp = "Bool.false"
let name = "1"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.is_nat.2: TestSuite
let test = "0865a"
let got = Bool.show(String.is_nat(test))
let exp = "Bool.false"
let name = "2"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.is_nat.3: TestSuite
let test = "9007199254740991n"
let got = Bool.show(String.is_nat(test))
let exp = "Bool.false"
let name = "3"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.is_nat.4: TestSuite
let test = "011101011101"
let got = Bool.show(String.is_nat(test))
let exp = "Bool.true"
let name = "4"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.is_nat.5: TestSuite
let test = "2.3847623487236483e+24"
let got = Bool.show(String.is_nat(test))
let exp = "Bool.false"
let name = "5"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)

View File

@ -16,49 +16,49 @@ String.test.to_hex.0: TestSuite
let got = String.to_hex(test)
let exp = "616263646566"
let name = "0"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.1: TestSuite
let test = ""
let got = String.to_hex(test)
let exp = ""
let name = "1"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.2: TestSuite
let test = "á"
let got = String.to_hex(test)
let exp = "c3a1"
let name = "2"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.3: TestSuite
let test = "ü"
let got = String.to_hex(test)
let exp = "c3bc"
let name = "3"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.4: TestSuite
let test = "õ"
let got = String.to_hex(test)
let exp = "c3b5"
let name = "4"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.5: TestSuite
let test = "1234567890"
let got = String.to_hex(test)
let exp = "31323334353637383930"
let name = "5"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
String.test.to_hex.6: TestSuite
let test = "{%$@"
let got = String.to_hex(test)
let exp = "7b252440"
let name = "6"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
// 2 bytes UTF-8
String.test.to_hex.7: TestSuite
@ -66,7 +66,7 @@ String.test.to_hex.7: TestSuite
let got = String.to_hex(test)
let exp = "c2a2"
let name = "7"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
// 3 bytes UTF-8
String.test.to_hex.8: TestSuite
@ -74,7 +74,7 @@ String.test.to_hex.8: TestSuite
let got = String.to_hex(test)
let exp = "e0a4b9e282ac"
let name = "8"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)
// 4 bytes UTF-8
String.test.to_hex.9: TestSuite
@ -82,4 +82,4 @@ String.test.to_hex.9: TestSuite
let got = String.to_hex(test)
let exp = "f0908d88"
let name = "9"
TestSuite.show.string(name, test, exp, got)
TestSuite.show.log_string(name, test, exp, got)

View File

@ -0,0 +1,81 @@
// Tests using NodeJS as reference
String.test.to_u16: TestSuite
TestSuite.many("String.to_u16", [
String.test.to_u16.0, String.test.to_u16.1, String.test.to_u16.2,
String.test.to_u16.3, //String.test.to_u16.4, //String.test.to_u16.5,
// String.test.to_u16.6, String.test.to_u16.7, String.test.to_u16.8,
// String.test.to_u16.9
])
String.test.to_u16.0: TestSuite
let test = "0"
let got = U16.show(String.to_u16(test))
let exp = "0"
let name = "0"
TestSuite.show.log_string(name, test, exp, got)
String.test.to_u16.1: TestSuite
let test = "12345"
let got = U16.show(String.to_u16(test))
let exp = "12345"
let name = "1"
TestSuite.show.log_string(name, test, exp, got)
String.test.to_u16.2: TestSuite
let test = "0007874"
let got = U16.show(String.to_u16(test))
let exp = "7874"
let name = "2"
TestSuite.show.log_string(name, test, exp, got)
String.test.to_u16.3: TestSuite
let test = "-42"
let got = U16.show(String.to_u16(test))
let exp = "42"
let name = "3"
TestSuite.show.log_string(name, test, exp, got)
// String.test.to_u16.4: TestSuite
// let test = "e0"
// let got = U16.show(String.to_u16(test))
// let exp = "224"
// let name = "4"
// TestSuite.show.log_string(name, test, exp, got)
// String.test.to_u16.5: TestSuite
// let test = "1234567890"
// let got = U16.show(String.to_u16(test))
// let exp = "31323334353637383930"
// let name = "5"
// TestSuite.show.log_string(name, test, exp, got)
// String.test.to_u16.6: TestSuite
// let test = "{%$@"
// let got = U16.show(String.to_u16(test))
// let exp = "7b252440"
// let name = "6"
// TestSuite.show.log_string(name, test, exp, got)
// // 2 bytes UTF-8
// String.test.to_u16.7: TestSuite
// let test = "¢"
// let got = U16.show(String.to_u16(test))
// let exp = "c2a2"
// let name = "7"
// TestSuite.show.log_string(name, test, exp, got)
// // 3 bytes UTF-8
// String.test.to_u16.8: TestSuite
// let test = "ह€"
// let got = U16.show(String.to_u16(test))
// let exp = "e0a4b9e282ac"
// let name = "8"
// TestSuite.show.log_string(name, test, exp, got)
// // 4 bytes UTF-8
// String.test.to_u16.9: TestSuite
// let test = "𐍈"
// let got = U16.show(String.to_u16(test))
// let exp = "f0908d88"
// let name = "9"
// TestSuite.show.log_string(name, test, exp, got)

5
base/String/to_char.kind Normal file
View File

@ -0,0 +1,5 @@
String.to_char(str: String): Char
case str {
nil : 0
cons: str.head
}

13
base/String/to_u16.kind Normal file
View File

@ -0,0 +1,13 @@
// Check if a String is a natural number
String.to_u16(s: String): U16
String.to_u16.aux(s, 0)
String.to_u16.aux(s: String, counter: U16): U16
let zero_ascii = 48#16
case s {
nil: counter
cons:
let new_c = (counter * 10) + (s.head - zero_ascii)
String.to_u16.aux(s.tail, new_c)
}

View File

@ -32,12 +32,12 @@ TestSuite.show.check_fail(s: TestSuite): String
result
}
TestSuite.log(name: String, test: String, expected: String, got: String): String
TestSuite.show.log(name: String, test: String, expected: String, got: String): String
"\n["|name|"] test: "|test|"\nexpected: "|expected|"\ngot: "|got
TestSuite.show.string(name: String, test: String, expected: String, got: String): TestSuite
TestSuite.show.log_string(name: String, test: String, expected: String, got: String): TestSuite
let res =
if String.eql(expected, got)
then Maybe.none!
else Maybe.some!(TestSuite.log(name, test, expected, got))
else Maybe.some!(TestSuite.show.log(name, test, expected, got))
TestSuite.unit(name, res)

View File

@ -1,3 +1,4 @@
// Values between 0 and 255 inclusive
type U8 {
new(value: Word(8))
}

8
base/User/maisa/bytes.md Normal file
View File

@ -0,0 +1,8 @@
# Bytes
Reference: https://docs.ethers.io/v5/api/utils/bytes/#Bytes
- A Bytes is any object which is an Array or TypedArray with each value in the valid byte range (i.e. between 0 and 255 inclusive), or is an Object with a length property where each indexed property is in the valid byte range.
#### Obs:
- All JavaScript numbers are 64-bit floating-point numbers

View File

@ -1,6 +0,0 @@
Package: kind-scm
Version: 1.0.93
Architecture: amd64
Maintainer: Rígille S. B. Menezes <rigillesbmenezes@protonmail.com>
Description: ChezScheme release of the kind programming language.
A minimal, efficient and practical proof and programming language. Under the hoods, it is basically Haskell, except purer and with dependent types. That means it can handle mathematical theorems just like Coq, Idris, Lean and Agda. On the surface, it aims to be more practical and looks more like TypeScript.

View File

@ -1,10 +0,0 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: kind-scm
Upstream-Contact: Rígille S. B. Menezes <rigillesbmenezes@protonmail.com>
Source: https://github.com/uwu-tech/Kind
Files: *
Copyright: 2016-2019 Ethereum Foundation
2019-2021 Sunshine Cybernetics
2021, Victor Maia
License: MIT