Skip to content

Commit b15b231

Browse files
committed
Change the behavior the Octal
- adds coverage - change argument types - use custom implementation for Base-8, Base-16
1 parent 38fe74e commit b15b231

28 files changed

+585
-267
lines changed

.github/workflows/test.yml

+84-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
1-
name: test
1+
name: Test
22

33
on:
44
workflow_dispatch:
55
push:
66
branches: [master]
7-
paths: ["**.dart", "**.yaml"]
7+
paths: ['**.dart', '**.yaml', '**.yml']
88
pull_request:
99
branches: [master]
10-
paths: ["**.dart", "**.yaml"]
11-
schedule:
12-
- cron: "0 0 * * 5" # m h d M w
10+
paths: ['**.dart', '**.yaml', '**.yml']
1311

1412
jobs:
15-
build:
13+
coverage:
1614
if: github.repository == 'bitanon/hashlib_codecs'
17-
runs-on: ${{ matrix.os }}
1815
strategy:
1916
matrix:
20-
os: [ubuntu-latest] # macos-latest, windows-latest
21-
sdk: ["2.14.0", "2.19.6", "stable", "dev"]
17+
os: [ubuntu-latest]
18+
sdk: ['stable']
19+
runs-on: ${{ matrix.os }}
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- uses: dart-lang/setup-dart@v1
24+
with:
25+
sdk: ${{ matrix.sdk }}
26+
27+
- name: 'Install global packages'
28+
run: |
29+
dart pub global activate coverage
30+
dart pub global activate cobertura
31+
dart pub global activate junitreport
32+
33+
- name: Run tests with coverage
34+
run: bash ./scripts/coverage.sh
35+
36+
- name: Upload results to Codecov
37+
uses: codecov/codecov-action@v4
38+
with:
39+
token: ${{ secrets.CODECOV_TOKEN }}
40+
file: ./coverage/cobertura.xml
2241

42+
- name: Upload test results to Codecov
43+
if: ${{ !cancelled() }}
44+
uses: codecov/test-results-action@v1
45+
with:
46+
token: ${{ secrets.CODECOV_TOKEN }}
47+
file: ./coverage/junit.xml
48+
49+
test-on-stable:
50+
strategy:
51+
matrix:
52+
os: [ubuntu-latest, macos-latest, windows-latest]
53+
sdk: ['stable']
54+
runs-on: ${{ matrix.os }}
2355
steps:
24-
- uses: actions/checkout@v3
56+
- uses: actions/checkout@v4
2557

26-
# You can specify other versions if desired, see documentation here:
27-
# https://github.com/dart-lang/setup-dart/blob/main/README.md
28-
- uses: dart-lang/[email protected]
58+
- uses: dart-lang/setup-dart@v1
2959
with:
3060
sdk: ${{ matrix.sdk }}
3161

@@ -38,12 +68,46 @@ jobs:
3868
- name: Analyze project source
3969
run: dart analyze --fatal-infos
4070

41-
- if: matrix.sdk != '2.14.0'
42-
name: Generate documentation
43-
run: dart doc --validate-links
71+
- name: Run tests
72+
run: dart test
73+
74+
test-on-legacy:
75+
strategy:
76+
matrix:
77+
os: [ubuntu-latest, windows-latest]
78+
sdk: ['2.14']
79+
runs-on: ${{ matrix.os }}
80+
steps:
81+
- uses: actions/checkout@v4
82+
83+
- uses: dart-lang/setup-dart@v1
84+
with:
85+
sdk: ${{ matrix.sdk }}
86+
87+
- name: Run tests
88+
run: dart test
89+
90+
test-on-beta:
91+
strategy:
92+
matrix:
93+
os: [ubuntu-latest]
94+
sdk: ['beta']
95+
runs-on: ${{ matrix.os }}
96+
steps:
97+
- uses: actions/checkout@v4
98+
99+
- uses: dart-lang/setup-dart@v1
100+
with:
101+
sdk: ${{ matrix.sdk }}
102+
103+
- name: Install dependencies
104+
run: dart pub get
44105

45-
- name: Run tests on vm
46-
run: dart test -p vm
106+
- name: Verify formatting
107+
run: dart format --output=none --set-exit-if-changed .
108+
109+
- name: Analyze project source
110+
run: dart analyze --fatal-infos
47111

48-
- name: Run tests on node
49-
run: dart test -p node
112+
- name: Run tests
113+
run: dart test

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
.dart_tool/
44
.packages
55

6-
# Conventional directory for build outputs.
6+
# Conventional directory for outputs.
77
build/
88
doc/
9+
coverage/
910
benchmark/**/*.exe
1011
test/**/*.exe
1112

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.6.0
2+
3+
- [**Breaking Changes**]
4+
- Change the behavior of the Base-8 encoder to follow the standard
5+
- Accept only `List<int>` instead of `Iterable<int>` in converters
6+
17
# 2.5.0
28

39
- Support UTF-8 encoding and decoding.

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Hashlib Codecs
22

33
[![plugin version](https://img.shields.io/pub/v/hashlib_codecs?label=pub)](https://pub.dev/packages/hashlib_codecs)
4-
[![dependencies](https://img.shields.io/badge/dependencies-zero-889)](https://github.com/bitanon/hashlib_codecs/blob/master/pubspec.yaml)
5-
[![dart support](https://img.shields.io/badge/dart-%3e%3d%202.14.0-39f?logo=dart)](https://dart.dev/guides/whats-new#september-8-2021-214-release)
4+
[![test](https://github.com/bitanon/hashlib_codecs/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/bitanon/hashlib_codecs/actions/workflows/test.yml)
5+
[![codecov](https://codecov.io/gh/bitanon/hashlib_codecs/graph/badge.svg?token=ISIYJ8MNI0)](https://codecov.io/gh/bitanon/hashlib_codecs)
66
[![likes](https://img.shields.io/pub/likes/hashlib_codecs?logo=dart)](https://pub.dev/packages/hashlib_codecs/score)
77
[![pub points](https://img.shields.io/pub/points/hashlib_codecs?logo=dart&color=teal)](https://pub.dev/packages/hashlib_codecs/score)
88
[![popularity](https://img.shields.io/pub/popularity/hashlib_codecs?logo=dart)](https://pub.dev/packages/hashlib_codecs/score)
9+
[![dart support](https://img.shields.io/badge/dart-%3e%3d%202.14.0-39f?logo=dart)](https://dart.dev/guides/whats-new#september-8-2021-214-release)
10+
[![dependencies](https://img.shields.io/badge/dependencies-zero-889)](https://github.com/bitanon/hashlib_codecs/blob/master/pubspec.yaml)
911

1012
<!-- [![test](https://github.com/bitanon/hashlib_codecs/actions/workflows/test.yml/badge.svg)](https://github.com/bitanon/hashlib_codecs/actions/workflows/test.yml) -->
1113

dart_test.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ concurrency: 8
55
platforms: [vm, node]
66

77
tags:
8-
skip-js:
8+
vm-only:
9+
skip: true
910
on_platform:
10-
node:
11-
skip: true
11+
vm:
12+
skip: false

lib/src/base16.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Base16Codec _codecFromParameters({
2323
/// - [codec] is the [Base16Codec] to use. It is derived from the other
2424
/// parameters if not provided.
2525
String toHex(
26-
Iterable<int> input, {
26+
List<int> input, {
2727
Base16Codec? codec,
2828
bool upper = false,
2929
}) {
@@ -49,6 +49,5 @@ Uint8List fromHex(
4949
Base16Codec? codec,
5050
}) {
5151
codec ??= _codecFromParameters();
52-
var out = codec.decoder.convert(input.codeUnits);
53-
return Uint8List.fromList(out as List<int>);
52+
return codec.decoder.convert(input.codeUnits);
5453
}

lib/src/base2.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'codecs/base2.dart';
1717
/// toBigInt(input).toRadixString(2)
1818
/// ```
1919
String toBinary(
20-
Iterable<int> input, {
20+
List<int> input, {
2121
Base2Codec codec = Base2Codec.standard,
2222
}) {
2323
var out = codec.encoder.convert(input);
@@ -44,6 +44,5 @@ Uint8List fromBinary(
4444
String input, {
4545
Base2Codec codec = Base2Codec.standard,
4646
}) {
47-
var out = codec.decoder.convert(input.codeUnits);
48-
return Uint8List.fromList(out as List<int>);
47+
return codec.decoder.convert(input.codeUnits);
4948
}

lib/src/base32.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const _codecsWithPadding = {
3838
/// - [codec] is the [Base32Codec] to use. It is derived from the other
3939
/// parameters if not provided.
4040
String toBase32(
41-
Iterable<int> input, {
41+
List<int> input, {
4242
Base32Codec? codec,
4343
bool lower = false,
4444
bool padding = true,
@@ -47,7 +47,7 @@ String toBase32(
4747
lower: lower,
4848
padding: padding,
4949
);
50-
var out = codec.encoder.convert(input);
50+
Iterable<int> out = codec.encoder.convert(input);
5151
if (!padding && _codecsWithPadding.contains(codec)) {
5252
out = out.takeWhile((x) => x != codec!.encoder.padding);
5353
}
@@ -76,5 +76,5 @@ Uint8List fromBase32(
7676
}) {
7777
codec ??= _codecFromParameters(padding: padding);
7878
var out = codec.decoder.convert(input.codeUnits);
79-
return Uint8List.fromList(out as List<int>);
79+
return Uint8List.fromList(out);
8080
}

lib/src/base64.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const _codecsWithPadding = {
3434
/// - [codec] is the [Base64Codec] to use. It is derived from the other
3535
/// parameters if not provided.
3636
String toBase64(
37-
Iterable<int> input, {
37+
List<int> input, {
3838
Base64Codec? codec,
3939
bool url = false,
4040
bool padding = true,
@@ -43,7 +43,7 @@ String toBase64(
4343
url: url,
4444
padding: padding,
4545
);
46-
var out = codec.encoder.convert(input);
46+
Iterable<int> out = codec.encoder.convert(input);
4747
if (!padding && _codecsWithPadding.contains(codec)) {
4848
out = out.takeWhile((x) => x != codec!.encoder.padding);
4949
}
@@ -73,5 +73,5 @@ Uint8List fromBase64(
7373
}) {
7474
codec ??= _codecFromParameters(padding: padding);
7575
var out = codec.decoder.convert(input.codeUnits);
76-
return Uint8List.fromList(out as List<int>);
76+
return Uint8List.fromList(out);
7777
}

lib/src/base8.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'codecs/base8.dart';
1717
/// toBigInt(input).toRadixString(8)
1818
/// ```
1919
String toOctal(
20-
Iterable<int> input, {
20+
List<int> input, {
2121
Base8Codec codec = Base8Codec.standard,
2222
}) {
2323
var out = codec.encoder.convert(input);
@@ -44,6 +44,5 @@ Uint8List fromOctal(
4444
String input, {
4545
Base8Codec codec = Base8Codec.standard,
4646
}) {
47-
var out = codec.decoder.convert(input.codeUnits);
48-
return Uint8List.fromList(out as List<int>);
47+
return codec.decoder.convert(input.codeUnits);
4948
}

lib/src/codecs/base16.dart

+43-41
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,32 @@ import 'dart:typed_data';
66
import 'package:hashlib_codecs/src/core/byte.dart';
77
import 'package:hashlib_codecs/src/core/codec.dart';
88

9-
// ========================================================
10-
// Base-16 Encoder and Decoder
11-
// ========================================================
12-
139
const int _zero = 0x30;
1410
const int _bigA = 0x41;
1511
const int _smallA = 0x61;
1612

13+
extension on List<int> {
14+
@pragma('vm:prefer-inline')
15+
int dec(int p) {
16+
int x = this[p] & 0xFF;
17+
if (x >= _smallA) {
18+
x -= _smallA - 10;
19+
} else if (x >= _bigA) {
20+
x -= _bigA - 10;
21+
} else {
22+
x -= _zero;
23+
}
24+
if (x < 0 || x > 15) {
25+
throw FormatException('Invalid character at $p');
26+
}
27+
return x;
28+
}
29+
}
30+
31+
// ========================================================
32+
// Base-16 Encoder and Decoder
33+
// ========================================================
34+
1735
class _Base16Encoder extends ByteEncoder {
1836
final int startCode;
1937

@@ -23,58 +41,42 @@ class _Base16Encoder extends ByteEncoder {
2341
static const lower = _Base16Encoder._(_smallA - 10);
2442

2543
@override
26-
Iterable<int> convert(Iterable<int> input) {
44+
Uint8List convert(List<int> input) {
2745
int i, p, x, a, b;
28-
List<int> list = input is List<int> ? input : List.of(input);
29-
var result = Uint8List(list.length << 1);
30-
for (i = p = 0; p < list.length; p++, i += 2) {
31-
x = list[p];
46+
var out = Uint8List(input.length << 1);
47+
for (i = p = 0; p < input.length; p++, i += 2) {
48+
x = input[p];
3249
a = (x >>> 4) & 0xF;
3350
b = x & 0xF;
3451
a += a < 10 ? _zero : startCode;
3552
b += b < 10 ? _zero : startCode;
36-
result[i] = a;
37-
result[i + 1] = b;
53+
out[i] = a;
54+
out[i + 1] = b;
3855
}
39-
return result;
56+
return out;
4057
}
4158
}
4259

4360
class _Base16Decoder extends ByteDecoder {
4461
const _Base16Decoder() : super(bits: 4);
4562

4663
@override
47-
Iterable<int> convert(Iterable<int> input) {
48-
bool t;
49-
int p, x, y;
50-
p = 0;
51-
t = false;
52-
List<int> out = <int>[];
53-
for (y in input) {
54-
if (y >= _smallA) {
55-
x = y - _smallA + 10;
56-
} else if (y >= _bigA) {
57-
x = y - _bigA + 10;
58-
} else if (y >= _zero) {
59-
x = y - _zero;
60-
} else {
61-
x = -1;
62-
}
63-
if (x < 0 || x > 15) {
64-
throw FormatException('Invalid character $y');
65-
}
66-
if (t) {
67-
out.add((p << 4) | x);
68-
p = 0;
69-
t = false;
70-
} else {
71-
p = x;
72-
t = true;
73-
}
64+
Uint8List convert(List<int> encoded) {
65+
int p, n;
66+
67+
n = encoded.length;
68+
p = (n >>> 1) + (n & 1);
69+
var out = Uint8List(p);
70+
71+
for (p--; n >= 2; n -= 2, p--) {
72+
out[p] = encoded.dec(n - 1) ^ //
73+
(encoded.dec(n - 2) << 4);
7474
}
75-
if (t) {
76-
out.add(p);
75+
76+
if (n == 1) {
77+
out[p] = encoded.dec(0);
7778
}
79+
7880
return out;
7981
}
8082
}

0 commit comments

Comments
 (0)