diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-08 22:14:56 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-08 22:14:56 +0100 |
commit | 29398e1848b0615afda30fb1da4ec8d81eccc1c5 (patch) | |
tree | 5762a473ca246cee13adb1632b7d5850a91fd78c /src/gleam/bit_string.gleam | |
parent | c5a001977a3b76f257a3a7ea4605f6ff61f1a590 (diff) | |
download | gleam_stdlib-29398e1848b0615afda30fb1da4ec8d81eccc1c5.tar.gz gleam_stdlib-29398e1848b0615afda30fb1da4ec8d81eccc1c5.zip |
Bit string slice
Diffstat (limited to 'src/gleam/bit_string.gleam')
-rw-r--r-- | src/gleam/bit_string.gleam | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/gleam/bit_string.gleam b/src/gleam/bit_string.gleam index 6605ce9..8ed94f8 100644 --- a/src/gleam/bit_string.gleam +++ b/src/gleam/bit_string.gleam @@ -45,19 +45,38 @@ pub fn append(to first: BitString, suffix second: BitString) -> BitString { concat([first, second]) } +/// Extracts a sub-section of a bit string. +/// +/// The slice will start at given position and continue up to specified +/// length. +/// A negative length can be used to extract bytes at the end of a bit string. +/// +/// This function runs in constant time. +/// +pub fn slice( + from string: BitString, + at position: Int, + take length: Int, +) -> Result(BitString, Nil) { + do_slice(string, position, length) +} + if erlang { - /// Extracts part of a bit string. - /// - /// BitString part will start at given position and continue up to specified - /// length. - /// A negative length can be used to extract bytes at the end of a bit string. - /// - pub external fn part( + pub external fn do_slice( + string: BitString, + position: Int, + length: Int, + ) -> Result(BitString, Nil) = + "gleam_stdlib" "bit_string_slice" +} + +if javascript { + pub external fn do_slice( string: BitString, position: Int, length: Int, ) -> Result(BitString, Nil) = - "gleam_stdlib" "bit_string_part_" + "../gleam_stdlib.js" "bit_string_slice" } /// Tests to see whether a bit string is valid UTF-8. |