-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strings): implement string builder for JS (#162)
- Loading branch information
Showing
5 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
package builtin | ||
|
||
struct string { | ||
pub: | ||
pub mut: | ||
str #JS.String | ||
length i32 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package strings | ||
|
||
type Builder := []u8 | ||
|
||
pub fun new_builder(initial_size i32) Builder { | ||
return []u8 as Builder | ||
} | ||
|
||
pub fun (mut b Builder) clear() { | ||
#JS.'b.data = [] | ||
b.length = 0' | ||
} | ||
|
||
pub fun (mut b Builder) write(data []u8) { | ||
b.push_many(data) | ||
} | ||
|
||
pub fun (mut b Builder) write_str(s string) { | ||
for c in s { | ||
b.push(c) | ||
} | ||
} | ||
|
||
pub fun (b Builder) str() string { | ||
return string{ | ||
str = #JS.'String.fromCharCode(...b.data)' as #JS.String | ||
length = b.length | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
import strings | ||
|
||
fun test_builder() { | ||
mut b := strings.new_builder(50) | ||
|
||
b.write([104 as u8, 101 as u8]) // FIXME second u8 cast should not be needed | ||
assert b.str() == 'he' | ||
|
||
b.write_str('llo') | ||
assert b.str() == 'hello' | ||
|
||
b.clear() | ||
assert b.str().length == 0 | ||
|
||
b.write_str('foo') | ||
assert b.str() == 'foo' | ||
} |