Packages

Gleam library to decode Windows-1250 (cp1250) binary data into a native string.

Current section

Files

Jump to
cp1250 src cp1250.erl
Raw

src/cp1250.erl

-module(cp1250).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/cp1250.gleam").
-export([decode/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" This library decodes cp1250 binary data into a native string\n"
" Based on https://github.com/richard-viney/iso_8859\n"
).
-file("src/cp1250.gleam", 15).
-spec bits_to_codepoints(bitstring(), bitstring(), list(integer())) -> list(integer()).
bits_to_codepoints(Bytes, Lut, Acc) ->
case Bytes of
<<Next_byte, Rest/binary>> ->
Codepoint@1 = case gleam_stdlib:bit_array_slice(
Lut,
Next_byte * 2,
2
) of
{ok, <<Codepoint:16>>} -> Codepoint;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"cp1250"/utf8>>,
function => <<"bits_to_codepoints"/utf8>>,
line => 22,
value => _assert_fail,
start => 539,
'end' => 611,
pattern_start => 550,
pattern_end => 570})
end,
Native_codepoint@1 = case gleam@string:utf_codepoint(Codepoint@1) of
{ok, Native_codepoint} -> Native_codepoint;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"cp1250"/utf8>>,
function => <<"bits_to_codepoints"/utf8>>,
line => 23,
value => _assert_fail@1,
start => 618,
'end' => 683,
pattern_start => 629,
pattern_end => 649})
end,
bits_to_codepoints(Rest, Lut, [Native_codepoint@1 | Acc]);
_ ->
Acc
end.
-file("src/cp1250.gleam", 9).
?DOC(" Convert cp1250 binary data to a native string\n").
-spec decode(bitstring()) -> binary().
decode(Bytes) ->
_pipe = bits_to_codepoints(
Bytes,
<<16#0000:16,
16#0001:16,
16#0002:16,
16#0003:16,
16#0004:16,
16#0005:16,
16#0006:16,
16#0007:16,
16#0008:16,
16#0009:16,
16#000A:16,
16#000B:16,
16#000C:16,
16#000D:16,
16#000E:16,
16#000F:16,
16#0010:16,
16#0011:16,
16#0012:16,
16#0013:16,
16#0014:16,
16#0015:16,
16#0016:16,
16#0017:16,
16#0018:16,
16#0019:16,
16#001A:16,
16#001B:16,
16#001C:16,
16#001D:16,
16#001E:16,
16#001F:16,
16#0020:16,
16#0021:16,
16#0022:16,
16#0023:16,
16#0024:16,
16#0025:16,
16#0026:16,
16#0027:16,
16#0028:16,
16#0029:16,
16#002A:16,
16#002B:16,
16#002C:16,
16#002D:16,
16#002E:16,
16#002F:16,
16#0030:16,
16#0031:16,
16#0032:16,
16#0033:16,
16#0034:16,
16#0035:16,
16#0036:16,
16#0037:16,
16#0038:16,
16#0039:16,
16#003A:16,
16#003B:16,
16#003C:16,
16#003D:16,
16#003E:16,
16#003F:16,
16#0040:16,
16#0041:16,
16#0042:16,
16#0043:16,
16#0044:16,
16#0045:16,
16#0046:16,
16#0047:16,
16#0048:16,
16#0049:16,
16#004A:16,
16#004B:16,
16#004C:16,
16#004D:16,
16#004E:16,
16#004F:16,
16#0050:16,
16#0051:16,
16#0052:16,
16#0053:16,
16#0054:16,
16#0055:16,
16#0056:16,
16#0057:16,
16#0058:16,
16#0059:16,
16#005A:16,
16#005B:16,
16#005C:16,
16#005D:16,
16#005E:16,
16#005F:16,
16#0060:16,
16#0061:16,
16#0062:16,
16#0063:16,
16#0064:16,
16#0065:16,
16#0066:16,
16#0067:16,
16#0068:16,
16#0069:16,
16#006A:16,
16#006B:16,
16#006C:16,
16#006D:16,
16#006E:16,
16#006F:16,
16#0070:16,
16#0071:16,
16#0072:16,
16#0073:16,
16#0074:16,
16#0075:16,
16#0076:16,
16#0077:16,
16#0078:16,
16#0079:16,
16#007A:16,
16#007B:16,
16#007C:16,
16#007D:16,
16#007E:16,
16#007F:16,
16#20AC:16,
16#0081:16,
16#201A:16,
16#0083:16,
16#201E:16,
16#2026:16,
16#2020:16,
16#2021:16,
16#0088:16,
16#2030:16,
16#0160:16,
16#2039:16,
16#015A:16,
16#0164:16,
16#017D:16,
16#0179:16,
16#0090:16,
16#2018:16,
16#2019:16,
16#201C:16,
16#201D:16,
16#2022:16,
16#2013:16,
16#2014:16,
16#0098:16,
16#2122:16,
16#0161:16,
16#203A:16,
16#015B:16,
16#0165:16,
16#017E:16,
16#017A:16,
16#00A0:16,
16#02C7:16,
16#02D8:16,
16#0141:16,
16#00A4:16,
16#0104:16,
16#00A6:16,
16#00A7:16,
16#00A8:16,
16#00A9:16,
16#015E:16,
16#00AB:16,
16#00AC:16,
16#00AD:16,
16#00AE:16,
16#017B:16,
16#00B0:16,
16#00B1:16,
16#02DB:16,
16#0142:16,
16#00B4:16,
16#00B5:16,
16#00B6:16,
16#00B7:16,
16#00B8:16,
16#0105:16,
16#015F:16,
16#00BB:16,
16#013D:16,
16#02DD:16,
16#013E:16,
16#017C:16,
16#0154:16,
16#00C1:16,
16#00C2:16,
16#0102:16,
16#00C4:16,
16#0139:16,
16#0106:16,
16#00C7:16,
16#010C:16,
16#00C9:16,
16#0118:16,
16#00CB:16,
16#011A:16,
16#00CD:16,
16#00CE:16,
16#010E:16,
16#0110:16,
16#0143:16,
16#0147:16,
16#00D3:16,
16#00D4:16,
16#0150:16,
16#00D6:16,
16#00D7:16,
16#0158:16,
16#016E:16,
16#00DA:16,
16#0170:16,
16#00DC:16,
16#00DD:16,
16#0162:16,
16#00DF:16,
16#0155:16,
16#00E1:16,
16#00E2:16,
16#0103:16,
16#00E4:16,
16#013A:16,
16#0107:16,
16#00E7:16,
16#010D:16,
16#00E9:16,
16#0119:16,
16#00EB:16,
16#011B:16,
16#00ED:16,
16#00EE:16,
16#010F:16,
16#0111:16,
16#0144:16,
16#0148:16,
16#00F3:16,
16#00F4:16,
16#0151:16,
16#00F6:16,
16#00F7:16,
16#0159:16,
16#016F:16,
16#00FA:16,
16#0171:16,
16#00FC:16,
16#00FD:16,
16#0163:16,
16#02D9:16>>,
[]
),
_pipe@1 = lists:reverse(_pipe),
gleam_stdlib:utf_codepoint_list_to_string(_pipe@1).