Browse Source

Implemented std:bytes:lzw:encode and std:bytes:lzw:decode

typed
Weird Constructor 6 months ago
parent
commit
bea0f8c335
  1. 2
      CHANGELOG.md
  2. 37
      doc/wlambda_reference.md
  3. 43
      src/cmdline_doc.json
  4. 37
      src/prelude.rs
  5. 6
      tests/language.rs

2
CHANGELOG.md

@ -12,6 +12,8 @@ for adding WLambda sources at Rust compile time, for easier deploying of the
Rust binary later.
* **Feature:** Access to environment variables is now possible using `std:sys:env:var`.
* **Feature:** Added `std:str:strip_utf8_bom`.
* **Feature:** Added default compression algorithm LZW to WLambda,
available via `std:bytes:lzw:encode` and `std:bytes:lzw:decode`.
0.8.1 (2022-03-05)
==================

37
doc/wlambda_reference.md

@ -198,6 +198,8 @@ Smalltalk, LISP and Perl.
- [3.11.11](#31111-stdbytestovec-byte-vector) std:bytes:to\_vec _byte-vector_
- [3.11.12](#31112-stdbytespack-pack-format-string-list-of-values) std:bytes:pack _pack-format-string_ _list-of-values_
- [3.11.13](#31113-stdbytesunpack-pack-format-string-byte-vector) std:bytes:unpack _pack-format-string_ _byte-vector_
- [3.11.14](#31114-stdbyteslzwencode-bytes-or-string-bitsize-bitorder---bytes--error) std:bytes:lzw:encode _bytes-or-string_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
- [3.11.15](#31115-stdbyteslzwdecode-bytes-bitsize-bitorder---bytes--error) std:bytes:lzw:decode _bytes_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
- [3.12](#312-symbols) Symbols
- [3.12.1](#3121-sym-value) sym _value_
- [3.12.2](#3122-issym-value) is\_sym _value_
@ -3832,6 +3834,41 @@ std:assert_str_eq
$[16, $b"ABC", $b"XY", $b"This is the rest"];
```
#### <a name="31114-stdbyteslzwencode-bytes-or-string-bitsize-bitorder---bytes--error"></a>3.11.14 - std:bytes:lzw:encode _bytes-or-string_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
This function encodes the given _bytes-or-string_ using the LZW algorithm. You can
specify a _bitsize_ which is by default 9. It has to be within (or equal) 2 and 12.
You can also specific the bitorder (`:msb` or `:lsb`).
If the _bitsize_ is not sufficient for encoding the given data, an `$error` is returned.
```wlambda
std:assert_eq len[std:bytes:lzw:encode $b"123123123123123123123123123123123123"] 20;
std:assert_eq len[std:bytes:lzw:encode $b"123123123123123123123123123123123123" 6] 14;
!text = "123123123123123123123123123123123123";
!data = std:bytes:lzw:encode text;
std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data]] text;
```
#### <a name="31115-stdbyteslzwdecode-bytes-bitsize-bitorder---bytes--error"></a>3.11.15 - std:bytes:lzw:decode _bytes_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
This function decodes the given _bytes_ and decodes them according to the LZW algorithm.
Make sure to set the right _bitsize_ and _bitorder_!
If there is some error in the data, an `$error` is returned.
The decoded data as bytes is returned otherwise. Here is an example:
```wlambda
!text = "123123123123123123123123123123123123";
!data = std:bytes:lzw:encode text 6;
std:assert_eq len[data] 14;
std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data, 6]] text;
```
### <a name="312-symbols"></a>3.12 - Symbols
Symbols are a special kind of strings that are interned by the runtime. That

43
src/cmdline_doc.json

@ -4013,6 +4013,49 @@
]
],
[
"3.11.14 - std:bytes:lzw:encode _bytes-or-string_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error",
[
"",
"This function encodes the given _bytes-or-string_ using the LZW algorithm. You can",
"specify a _bitsize_ which is by default 9. It has to be within (or equal) 2 and 12.",
"You can also specific the bitorder (`:msb` or `:lsb`).",
"",
"If the _bitsize_ is not sufficient for encoding the given data, an `$error` is returned.",
"",
"```wlambda",
"std:assert_eq len[std:bytes:lzw:encode $b\"123123123123123123123123123123123123\"] 20;",
"std:assert_eq len[std:bytes:lzw:encode $b\"123123123123123123123123123123123123\" 6] 14;",
"",
"!text = \"123123123123123123123123123123123123\";",
"!data = std:bytes:lzw:encode text;",
"",
"std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data]] text;",
"```",
""
]
],
[
"3.11.15 - std:bytes:lzw:decode _bytes_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error",
[
"",
"This function decodes the given _bytes_ and decodes them according to the LZW algorithm.",
"Make sure to set the right _bitsize_ and _bitorder_!",
"",
"If there is some error in the data, an `$error` is returned.",
"",
"The decoded data as bytes is returned otherwise. Here is an example:",
"",
"```wlambda",
"!text = \"123123123123123123123123123123123123\";",
"!data = std:bytes:lzw:encode text 6;",
"std:assert_eq len[data] 14;",
"",
"std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data, 6]] text;",
"```",
""
]
],
[
"3.12 - Symbols",
[
"",

37
src/prelude.rs

@ -212,6 +212,8 @@ Smalltalk, LISP and Perl.
- [3.11.11](#31111-stdbytestovec-byte-vector) std:bytes:to\_vec _byte-vector_
- [3.11.12](#31112-stdbytespack-pack-format-string-list-of-values) std:bytes:pack _pack-format-string_ _list-of-values_
- [3.11.13](#31113-stdbytesunpack-pack-format-string-byte-vector) std:bytes:unpack _pack-format-string_ _byte-vector_
- [3.11.14](#31114-stdbyteslzwencode-bytes-or-string-bitsize-bitorder---bytes--error) std:bytes:lzw:encode _bytes-or-string_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
- [3.11.15](#31115-stdbyteslzwdecode-bytes-bitsize-bitorder---bytes--error) std:bytes:lzw:decode _bytes_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
- [3.12](#312-symbols) Symbols
- [3.12.1](#3121-sym-value) sym _value_
- [3.12.2](#3122-issym-value) is\_sym _value_
@ -3846,6 +3848,41 @@ std:assert_str_eq
$[16, $b"ABC", $b"XY", $b"This is the rest"];
```
#### <a name="31114-stdbyteslzwencode-bytes-or-string-bitsize-bitorder---bytes--error"></a>3.11.14 - std:bytes:lzw:encode _bytes-or-string_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
This function encodes the given _bytes-or-string_ using the LZW algorithm. You can
specify a _bitsize_ which is by default 9. It has to be within (or equal) 2 and 12.
You can also specific the bitorder (`:msb` or `:lsb`).
If the _bitsize_ is not sufficient for encoding the given data, an `$error` is returned.
```wlambda
std:assert_eq len[std:bytes:lzw:encode $b"123123123123123123123123123123123123"] 20;
std:assert_eq len[std:bytes:lzw:encode $b"123123123123123123123123123123123123" 6] 14;
!text = "123123123123123123123123123123123123";
!data = std:bytes:lzw:encode text;
std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data]] text;
```
#### <a name="31115-stdbyteslzwdecode-bytes-bitsize-bitorder---bytes--error"></a>3.11.15 - std:bytes:lzw:decode _bytes_ [_bitsize_ [_bitorder_]] -> _bytes_ | $error
This function decodes the given _bytes_ and decodes them according to the LZW algorithm.
Make sure to set the right _bitsize_ and _bitorder_!
If there is some error in the data, an `$error` is returned.
The decoded data as bytes is returned otherwise. Here is an example:
```wlambda
!text = "123123123123123123123123123123123123";
!data = std:bytes:lzw:encode text 6;
std:assert_eq len[data] 14;
std:assert_eq std:str:from_utf8[std:bytes:lzw:decode[data, 6]] text;
```
### <a name="312-symbols"></a>3.12 - Symbols
Symbols are a special kind of strings that are interned by the runtime. That

6
tests/language.rs

@ -5947,3 +5947,9 @@ fn check_prelude_deflate() {
assert_eq!(ve("std:bytes:gzip:decode ~ std:bytes:gzip:encode $q'AAAAAAAAAAAAAAAAAAAAAAAAAA'"), "$b\"AAAAAAAAAAAAAAAAAAAAAAAAAA\"");
}
}
#[test]
fn check_prelude_lzw() {
assert_eq!(ve("len ~ std:bytes:lzw:encode $q'AAAAAAAAAAAAAAAAAAAAAAAAAA'"), "12");
assert_eq!(ve("std:bytes:lzw:decode ~ std:bytes:lzw:encode $q'AAAAAAAAAAAAAAAAAAAAAAAAAA'"), "$b\"AAAAAAAAAAAAAAAAAAAAAAAAAA\"");
}

Loading…
Cancel
Save