forked from pen-lang/pen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.pen
39 lines (33 loc) · 1015 Bytes
/
String.pen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# This module provides common string operations.
import foreign "c" _pen_core_string_has_prefix \(string, string) boolean
import foreign "c" _pen_core_string_builder_create \() stringBuilder
import foreign "c" _pen_core_string_builder_append \(stringBuilder, string) stringBuilder
import foreign "c" _pen_core_string_builder_build \(stringBuilder, string) string
type stringBuilder {
value any
}
# Concatenate strings.
Concatenate = \(strings [string]) string {
Join(strings, "")
}
# Join strings with a separator.
Join = \(strings [string], separator string) string {
_pen_core_string_builder_build(
appendStrings(_pen_core_string_builder_create(), strings),
separator,
)
}
appendStrings = \(b stringBuilder, ss [string]) stringBuilder {
if [s, ...ss] = ss {
appendStrings(
_pen_core_string_builder_append(b, s()),
ss,
)
} else {
b
}
}
# Check if a string has a prefix.
HasPrefix = \(s string, prefix string) boolean {
_pen_core_string_has_prefix(s, prefix)
}