aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-07-28 19:00:08 +0100
committerLouis Pilfold <louis@lpil.uk>2021-07-28 19:00:08 +0100
commit838904656a8b38d32a1ab897b93ab35f8b688118 (patch)
tree6f1a4a2cf6cae31f7afe8bdd69d216fe44cb3898
parent4b1228b7ebc4a2355678f0eb47afacd842e531b3 (diff)
downloadgleam_stdlib-838904656a8b38d32a1ab897b93ab35f8b688118.tar.gz
gleam_stdlib-838904656a8b38d32a1ab897b93ab35f8b688118.zip
Bit string JS functions
-rw-r--r--src/gleam/bit_string.gleam61
-rw-r--r--src/gleam/io.gleam61
-rw-r--r--src/gleam_stdlib.js19
-rw-r--r--test/gleam/bit_string_test.gleam39
4 files changed, 123 insertions, 57 deletions
diff --git a/src/gleam/bit_string.gleam b/src/gleam/bit_string.gleam
index 8611c46..fc6cf58 100644
--- a/src/gleam/bit_string.gleam
+++ b/src/gleam/bit_string.gleam
@@ -2,27 +2,60 @@
//// The BitString type should be used instead of a String type when not utf8
//// encoded.
+/// Converts a UTF-8 String type into a raw BitString type.
+///
+pub fn from_string(x: String) -> BitString {
+ do_from_string(x)
+}
+
if erlang {
- /// Converts a UTF-8 String type into a raw BitString type.
- ///
- pub external fn from_string(String) -> BitString =
+ external fn do_from_string(String) -> BitString =
"gleam_stdlib" "identity"
+}
- /// Returns an integer which is the number of bytes in the bit string.
- ///
- pub external fn byte_size(BitString) -> Int =
+if javascript {
+ external fn do_from_string(String) -> BitString =
+ "../gleam_stdlib.js" "bit_string_from_string"
+}
+
+/// Returns an integer which is the number of bytes in the bit string.
+///
+pub fn byte_size(x: BitString) -> Int {
+ do_byte_size(x)
+}
+
+if erlang {
+ external fn do_byte_size(BitString) -> Int =
"erlang" "byte_size"
+}
- /// Creates a new bit string by joining two binaries.
- ///
- /// ## Examples
- ///
- /// > append(to: from_string("butter"), suffix: from_string("fly"))
- /// from_string("butterfly")
- ///
- pub external fn append(first: BitString, second: BitString) -> BitString =
+if javascript {
+ external fn do_byte_size(BitString) -> Int =
+ "../gleam_stdlib.js" "byte_size"
+}
+
+/// Creates a new bit string by joining two binaries.
+///
+/// ## Examples
+///
+/// > append(to: from_string("butter"), suffix: from_string("fly"))
+/// from_string("butterfly")
+///
+pub fn append(to first: BitString, suffix second: BitString) -> BitString {
+ do_append(first, second)
+}
+
+if erlang {
+ external fn do_append(BitString, BitString) -> BitString =
"gleam_stdlib" "bit_string_append"
+}
+if javascript {
+ external fn do_append(BitString, BitString) -> BitString =
+ "../gleam_stdlib.js" "bit_string_append"
+}
+
+if erlang {
/// Extracts part of a bit string.
///
/// BitString part will start at given position and continue up to specified
diff --git a/src/gleam/io.gleam b/src/gleam/io.gleam
index f6e7f40..9482843 100644
--- a/src/gleam/io.gleam
+++ b/src/gleam/io.gleam
@@ -29,34 +29,47 @@ if erlang {
erl_print("~ts\n", [string])
Nil
}
+}
- /// Prints a value to standard output using Erlang syntax.
- ///
- /// The value is returned after being printed so it can be used in pipelines.
- ///
- /// ## Example
- ///
- /// > io.debug("Hi mum")
- /// // -> <<"Hi mum">>
- /// "Hi mum"
- ///
- /// > io.debug(Ok(1))
- /// // -> {ok, 1}
- /// Ok(1)
- ///
- /// > import list
- /// > [1, 2]
- /// > |> list.map(fn(x) { x + 1 })
- /// > |> io.debug
- /// > |> list.map(fn(x) { x * 2 })
- /// // -> [2, 3]
- /// [4, 6]
- ///
- pub fn debug(term: anything) -> anything {
+/// Prints a value to standard output using Erlang syntax.
+///
+/// The value is returned after being printed so it can be used in pipelines.
+///
+/// ## Example
+///
+/// > io.debug("Hi mum")
+/// // -> <<"Hi mum">>
+/// "Hi mum"
+///
+/// > io.debug(Ok(1))
+/// // -> {ok, 1}
+/// Ok(1)
+///
+/// > import list
+/// > [1, 2]
+/// > |> list.map(fn(x) { x + 1 })
+/// > |> io.debug
+/// > |> list.map(fn(x) { x * 2 })
+/// // -> [2, 3]
+/// [4, 6]
+///
+pub fn debug(term: anything) -> anything {
+ debug_print(term)
+ term
+}
+
+if erlang {
+ fn debug_print(term: anything) -> DoNotLeak {
erl_print("~tp\n", [term])
- term
}
+}
+if javascript {
+ external fn debug_print(anything) -> Nil =
+ "../gleam_stdlib" "log"
+}
+
+if erlang {
/// Error value returned by `get_line` function
///
pub type GetLineError {
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 89908de..d18cd5e 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -107,7 +107,9 @@ export function join(xs) {
}
export function byte_size(data) {
- if (typeof Blob === "function") {
+ if (data instanceof ArrayBuffer) {
+ return data.byteLength;
+ } else if (typeof Blob === "function") {
return new Blob([data]).size;
} else if (typeof Buffer === "function") {
return Buffer.byteLength(data);
@@ -158,3 +160,18 @@ export function trim_left(string) {
export function trim_right(string) {
return string.trimRight();
}
+
+export function bit_string_from_string(string) {
+ return new TextEncoder().encode(string).buffer;
+}
+
+export function bit_string_append(first, second) {
+ let array = new Uint8Array(first.byteLength + second.byteLength);
+ array.set(new Uint8Array(first), 0);
+ array.set(new Uint8Array(second), first.byteLength);
+ return array.buffer;
+}
+
+export function log(term) {
+ console.log(term);
+}
diff --git a/test/gleam/bit_string_test.gleam b/test/gleam/bit_string_test.gleam
index a69bc90..8012587 100644
--- a/test/gleam/bit_string_test.gleam
+++ b/test/gleam/bit_string_test.gleam
@@ -1,26 +1,29 @@
-if erlang {
- import gleam/bit_string
- import gleam/should
+import gleam/bit_string
+import gleam/should
- pub fn length_test() {
- bit_string.byte_size(bit_string.from_string("hello"))
- |> should.equal(5)
+pub fn byte_size_test() {
+ bit_string.byte_size(bit_string.from_string("hello"))
+ |> should.equal(5)
- bit_string.byte_size(bit_string.from_string(""))
- |> should.equal(0)
- }
+ bit_string.byte_size(bit_string.from_string(""))
+ |> should.equal(0)
+}
- pub fn append_test() {
- bit_string.from_string("Test")
- |> bit_string.append(bit_string.from_string(" Me"))
- |> should.equal(bit_string.from_string("Test Me"))
+pub fn append_test() {
+ bit_string.from_string("Test")
+ |> bit_string.append(bit_string.from_string(" Me"))
+ |> should.equal(bit_string.from_string("Test Me"))
- let Ok(zero_32bit) = bit_string.int_to_u32(0)
- zero_32bit
- |> bit_string.append(bit_string.from_string(""))
- |> should.equal(zero_32bit)
- }
+ <<1, 2>>
+ |> bit_string.append(<<>>)
+ |> should.equal(<<1, 2>>)
+ <<1, 2>>
+ |> bit_string.append(<<3, 4>>)
+ |> should.equal(<<1, 2, 3, 4>>)
+}
+
+if erlang {
pub fn part_test() {
bit_string.from_string("hello")
|> bit_string.part(0, 5)