aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-12-09 09:10:36 +0100
committerLouis Pilfold <louis@lpil.uk>2022-12-22 20:44:33 +0000
commit9dc8bc4b3e9b58396ceae3e2bb466c1eb4679f86 (patch)
tree6f2e022109d024886f789dc6380e36be35d0a94e
parent48f47291d190ce02701a23a431fda9d8dab99ac1 (diff)
downloadgleam_stdlib-9dc8bc4b3e9b58396ceae3e2bb466c1eb4679f86.tar.gz
gleam_stdlib-9dc8bc4b3e9b58396ceae3e2bb466c1eb4679f86.zip
replace string.to_ints with string.utf_codepoint_to_int
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/string.gleam27
-rw-r--r--test/gleam/string_test.gleam28
3 files changed, 13 insertions, 44 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4b1f73a..3085f68 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,7 +15,7 @@
iterating over graphemes in older JavaScript environments that do not have the
`Intl.Segmenter` class.
- The `string` module gains `to_utf_codepoints`, `from_utf_codepoints`, and
- `to_ints` functions.
+ `utf_codepoint_to_int` functions.
## v0.25.0 - 2022-11-19
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index e2377e2..04d58a8 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -900,39 +900,26 @@ pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) {
}
}
-/// Converts a string to a list of UtfCodepoint integers.
+/// Converts an UtfCodepoint to its ordinal code point value.
///
/// ## Examples
///
/// ```gleam
-/// > "abc"
-/// > |> string.to_ints
-/// [97, 98, 99]
+/// > utf_codepoint_to_int(128013) |> to_utf_codepoint_int
+/// 128013
/// ```
///
-/// ```gleam
-/// > "🐍"
-/// > |> string.to_ints
-/// [128013]
-/// ```
-///
-/// ```gleam
-/// > [utf_codepoint(128013)] |> from_utf_codepoints |> to_ints
-/// "🐍"
-/// ```
-///
-pub fn to_ints(s: String) -> List(Int) {
- to_utf_codepoints(s)
- |> list.map(utf_codepoint_to_int)
+pub fn utf_codepoint_to_int(cp: UtfCodepoint) -> Int {
+ do_utf_codepoint_to_int(cp)
}
if erlang {
- external fn utf_codepoint_to_int(cp: UtfCodepoint) -> Int =
+ external fn do_utf_codepoint_to_int(cp: UtfCodepoint) -> Int =
"gleam_stdlib" "identity"
}
if javascript {
- external fn utf_codepoint_to_int(cp: UtfCodepoint) -> Int =
+ external fn do_utf_codepoint_to_int(cp: UtfCodepoint) -> Int =
"../gleam_stdlib.mjs" "utf_codepoint_to_int"
}
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index d8c62d6..07bbfb9 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -487,31 +487,13 @@ pub fn bit_string_utf_codepoint_test() {
should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>)
}
-pub fn to_ints_test() {
- ""
- |> string.to_ints
- |> should.equal([])
-
- "abc"
- |> string.to_ints
- |> should.equal([97, 98, 99])
-
- "🐍"
- |> string.to_ints
- |> should.equal([128013])
-
+pub fn utf_codepoint_to_int_test() {
{
- assert Ok(cp) = string.utf_codepoint(128013)
- [cp]
+ assert Ok(ordinal_value) = string.utf_codepoint(128_013)
+ ordinal_value
}
- |> string.from_utf_codepoints
- |> string.to_ints
- |> should.equal([128013])
-
- string.to_utf_codepoints("🐍")
- |> string.from_utf_codepoints
- |> string.to_ints
- "🐍"
+ |> string.utf_codepoint_to_int
+ |> should.equal(128_013)
}
pub fn to_option_test() {