Skip to content

Commit b692b3f

Browse files
committed
version bump 0.4.0
- simplified utf8 code - added browser demo - added command line tool adler32 - fixed unicode baseline script (node 6 changed default array printing) - fixed performance tests (benchmark module changed behavior) - updated travis versions for test - miscellaneous adjustments to tooling
1 parent 8215562 commit b692b3f

30 files changed

+651
-145
lines changed

.flowconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
.*/misc/.*
1010
.*/perf/.*
1111

12+
.*/demo/browser.js
13+
1214
[include]
1315
adler32.flow.js
16+
.*/demo/browser.flow.js
1417

1518
[libs]
19+
bits/10_types.js
20+
misc/flow.js
1621

1722
[options]

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ test_files/*.py
33
test_files/*.js
44
test_files/baseline*
55
misc/coverage.html
6-
misc/*/

.jshintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
"bitwise": false,
33
"curly": false
44
}
5-

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: node_js
22
node_js:
3-
- "5.0"
4-
- "4.2"
3+
- "6"
4+
- "5"
5+
- "4"
56
- "0.12"
67
- "0.10"
78
- "0.8"

Makefile

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,88 @@
11
LIB=adler32
22
REQS=
33
ADDONS=
4-
AUXTARGETS=
4+
AUXTARGETS=demo/browser.js
5+
HTMLLINT=index.html
56

67
ULIB=$(shell echo $(LIB) | tr a-z A-Z)
78
DEPS=$(sort $(wildcard bits/*.js))
89
TARGET=$(LIB).js
10+
FLOWTARGET=$(LIB).flow.js
11+
12+
## Main Targets
913

1014
.PHONY: all
11-
all: $(TARGET) $(AUXTARGETS)
15+
all: $(TARGET) $(AUXTARGETS) ## Build library and auxiliary scripts
1216

1317
$(TARGET) $(AUXTARGETS): %.js : %.flow.js
14-
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^\s*\/\*:[^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*:[^*]*\*\//gm,""))' > $@
18+
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^[ \t]*\/\*[:#][^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*[:#][^*]*\*\//gm,""))' > $@
1519

16-
$(LIB).flow.js: $(DEPS)
20+
$(FLOWTARGET): $(DEPS)
1721
cat $^ | tr -d '\15\32' > $@
1822

1923
bits/01_version.js: package.json
2024
echo "ADLER32.version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@
2125

2226
.PHONY: clean
23-
clean: clean-baseline
24-
rm -f $(TARGET)
27+
clean: clean-baseline ## Remove targets and build artifacts
28+
rm -f $(TARGET) $(FLOWTARGET)
29+
30+
## Testing
2531

2632
.PHONY: test mocha
27-
test mocha: test.js $(TARGET) baseline
33+
test mocha: test.js $(TARGET) baseline ## Run test suite
2834
mocha -R spec -t 20000
2935

3036
.PHONY: ctest
31-
ctest:
37+
ctest: ## Build browser test (into ctest/ subdirectory)
3238
cat misc/*.js > ctest/fixtures.js
3339
cp -f test.js ctest/test.js
3440
cp -f $(TARGET) ctest/
3541

42+
.PHONY: ctestserv
43+
ctestserv: ## Start a test server on port 8000
44+
@cd ctest && python -mSimpleHTTPServer
45+
46+
.PHONY: baseline
47+
baseline: ## Build test baselines
48+
@bash ./misc/make_baseline.sh
49+
50+
.PHONY: clean-baseline
51+
clean-baseline: ## Remove test baselines
52+
@bash ./misc/make_baseline.sh clean
53+
54+
## Code Checking
55+
3656
.PHONY: lint
37-
lint: $(TARGET) $(AUXTARGETS)
38-
jshint --show-non-errors $(TARGET) $(AUXTARGETS)
39-
jshint --show-non-errors package.json
40-
jscs $(TARGET) $(AUXTARGETS)
57+
lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
58+
@jshint --show-non-errors $(TARGET) $(AUXTARGETS)
59+
@jshint --show-non-errors package.json
60+
@jshint --show-non-errors --extract=always $(HTMLLINT)
61+
@jscs $(TARGET) $(AUXTARGETS)
4162

4263
.PHONY: flow
43-
flow: lint
44-
flow check --all --show-all-errors
64+
flow: lint ## Run flow checker
65+
@flow check --all --show-all-errors
4566

46-
.PHONY: cov cov-spin
47-
cov: misc/coverage.html
48-
cov-spin:
49-
make cov & bash misc/spin.sh $$!
50-
51-
COVFMT=$(patsubst %,cov_%,$(FMT))
52-
.PHONY: $(COVFMT)
53-
$(COVFMT): cov_%:
54-
FMTS=$* make cov
67+
.PHONY: cov
68+
cov: misc/coverage.html ## Run coverage test
5569

5670
misc/coverage.html: $(TARGET) test.js
5771
mocha --require blanket -R html-cov -t 20000 > $@
5872

59-
.PHONY: coveralls coveralls-spin
60-
coveralls:
61-
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | ./node_modules/coveralls/bin/coveralls.js
62-
63-
coveralls-spin:
64-
make coveralls & bash misc/spin.sh $$!
73+
.PHONY: coveralls
74+
coveralls: ## Coverage Test + Send to coveralls.io
75+
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js
6576

6677
.PHONY: perf
67-
perf:
68-
bash perf/perf.sh
78+
perf: ## Run Performance Tests
79+
@bash perf/perf.sh
6980

70-
.PHONY: perf-all
71-
perf-all:
72-
bash misc/perf.sh
7381

74-
.PHONY: baseline clean-baseline
75-
baseline:
76-
./misc/make_baseline.sh
82+
.PHONY: help
83+
help:
84+
@grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST) | bash misc/help.sh
7785

78-
clean-baseline:
79-
rm -f test_files/*.*
86+
#* To show a spinner, append "-spin" to any target e.g. cov-spin
87+
%-spin:
88+
@make $* & bash misc/spin.sh $$!

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Emphasis on correctness and performance.
55

66
## Installation
77

8-
In [nodejs](https://www.npmjs.org/package/adler-32):
8+
With [npm](https://www.npmjs.org/package/adler-32):
99

1010
npm install adler-32
1111

@@ -15,6 +15,9 @@ In the browser:
1515

1616
The browser exposes a variable ADLER32
1717

18+
When installed globally, npm installs a script `adler32` that computes the
19+
checksum for a specified file or standard input.
20+
1821
## Usage
1922

2023
- `ADLER32.buf(byte array or buffer)` assumes the argument is a set of 8 bit
@@ -27,9 +30,12 @@ The browser exposes a variable ADLER32
2730

2831
## Testing
2932

30-
`make test` will run the nodejs-based test. To run the in-browser tests, run a
31-
local server and go to the `ctest` directory. To update the browser artifacts,
32-
run `make ctest`.
33+
`make test` will run the nodejs-based test.
34+
35+
To run the in-browser tests, run a local server and go to the `ctest` directory.
36+
`make ctestserv` will start a python `SimpleHTTPServer` server on port 8000.
37+
38+
To update the browser artifacts, run `make ctest`.
3339

3440
To generate the bits file, use the `adler32` function from python zlib:
3541

@@ -44,10 +50,25 @@ To generate the bits file, use the `adler32` function from python zlib:
4450
2023497376
4551
```
4652

53+
The included `adler32.njs` script can process files or stdin:
54+
55+
```
56+
$ echo "this is a test" > t.txt
57+
$ bin/adler32.njs t.txt
58+
726861088
59+
```
60+
61+
For comparison, the included `adler32.py` script uses python zlib:
62+
63+
```
64+
$ bin/adler32.py t.txt
65+
726861088
66+
```
67+
4768
## Performance
4869

4970
`make perf` will run algorithmic performance tests (which should justify certain
50-
decisions in the code).
71+
decisions in the code).
5172

5273
[js-crc](http://git.io/crc32) has more performance notes
5374

adler32.flow.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* adler32.js (C) 2014-present SheetJS -- http://sheetjs.com */
22
/* vim: set ts=2: */
3+
/*exported ADLER32 */
34
var ADLER32;
45
/*:: declare var DO_NOT_EXPORT_ADLER: any; */
56
/*:: declare var define: any; */
67
(function (factory) {
8+
/*jshint ignore:start */
79
if(typeof DO_NOT_EXPORT_ADLER === 'undefined') {
810
if('object' === typeof exports) {
911
factory(exports);
@@ -19,18 +21,20 @@ var ADLER32;
1921
} else {
2022
factory(ADLER32 = {});
2123
}
24+
/*jshint ignore:end */
2225
}(function(ADLER32) {
23-
ADLER32.version = '0.3.0';
26+
ADLER32.version = '0.4.0';
2427
/*::
2528
type ADLER32Type = number;
2629
type ABuf = Array<number> | Buffer;
2730
*/
28-
/* consult README.md for the magic number */
29-
/* charCodeAt is the best approach for binary strings */
31+
/*# consult README.md for the magic number */
32+
/*# charCodeAt is the best approach for binary strings */
33+
/*global Buffer */
3034
var use_buffer = typeof Buffer !== 'undefined';
3135
function adler32_bstr(bstr/*:string*/)/*:ADLER32Type*/ {
3236
if(bstr.length > 32768) if(use_buffer) return adler32_buf(new Buffer(bstr));
33-
var a = 1, b = 0, L = bstr.length, M;
37+
var a = 1, b = 0, L = bstr.length, M = 0;
3438
for(var i = 0; i < L;) {
3539
M = Math.min(L-i, 3850)+i;
3640
for(;i<M;i++) {
@@ -44,7 +48,7 @@ function adler32_bstr(bstr/*:string*/)/*:ADLER32Type*/ {
4448
}
4549

4650
function adler32_buf(buf/*:ABuf*/)/*:ADLER32Type*/ {
47-
var a = 1, b = 0, L = buf.length, M;
51+
var a = 1, b = 0, L = buf.length, M = 0;
4852
for(var i = 0; i < L;) {
4953
M = Math.min(L-i, 3850)+i;
5054
for(;i<M;i++) {
@@ -57,31 +61,32 @@ function adler32_buf(buf/*:ABuf*/)/*:ADLER32Type*/ {
5761
return ((b%65521) << 16) | (a%65521);
5862
}
5963

60-
/* much much faster to intertwine utf8 and adler */
64+
/*# much much faster to intertwine utf8 and adler */
6165
function adler32_str(str/*:string*/)/*:ADLER32Type*/ {
62-
var a = 1, b = 0, L = str.length, M, c, d;
66+
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
6367
for(var i = 0; i < L;) {
6468
M = Math.min(L-i, 3850);
6569
while(M>0) {
6670
c = str.charCodeAt(i++);
67-
if(c < 0x80) { a += c; b += a; --M; }
71+
if(c < 0x80) { a += c; }
6872
else if(c < 0x800) {
6973
a += 192|((c>>6)&31); b += a; --M;
70-
a += 128|(c&63); b += a; --M;
74+
a += 128|(c&63);
7175
} else if(c >= 0xD800 && c < 0xE000) {
7276
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
7377
a += 240|((c>>8)&7); b += a; --M;
7478
a += 128|((c>>2)&63); b += a; --M;
7579
a += 128|((d>>6)&15)|((c&3)<<4); b += a; --M;
76-
a += 128|(d&63); b += a; --M;
80+
a += 128|(d&63);
7781
} else {
7882
a += 224|((c>>12)&15); b += a; --M;
7983
a += 128|((c>>6)&63); b += a; --M;
80-
a += 128|(c&63); b += a; --M;
84+
a += 128|(c&63);
8185
}
86+
b += a; --M;
8287
}
83-
a %= 65521;
84-
b %= 65521;
88+
a = (15*(a>>>16)+(a&65535));
89+
b = (15*(b>>>16)+(b&65535));
8590
}
8691
return (b << 16) | a;
8792
}

adler32.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* adler32.js (C) 2014-present SheetJS -- http://sheetjs.com */
22
/* vim: set ts=2: */
3+
/*exported ADLER32 */
34
var ADLER32;
45
(function (factory) {
6+
/*jshint ignore:start */
57
if(typeof DO_NOT_EXPORT_ADLER === 'undefined') {
68
if('object' === typeof exports) {
79
factory(exports);
@@ -17,14 +19,14 @@ var ADLER32;
1719
} else {
1820
factory(ADLER32 = {});
1921
}
22+
/*jshint ignore:end */
2023
}(function(ADLER32) {
21-
ADLER32.version = '0.3.0';
22-
/* consult README.md for the magic number */
23-
/* charCodeAt is the best approach for binary strings */
24+
ADLER32.version = '0.4.0';
25+
/*global Buffer */
2426
var use_buffer = typeof Buffer !== 'undefined';
2527
function adler32_bstr(bstr) {
2628
if(bstr.length > 32768) if(use_buffer) return adler32_buf(new Buffer(bstr));
27-
var a = 1, b = 0, L = bstr.length, M;
29+
var a = 1, b = 0, L = bstr.length, M = 0;
2830
for(var i = 0; i < L;) {
2931
M = Math.min(L-i, 3850)+i;
3032
for(;i<M;i++) {
@@ -38,7 +40,7 @@ function adler32_bstr(bstr) {
3840
}
3941

4042
function adler32_buf(buf) {
41-
var a = 1, b = 0, L = buf.length, M;
43+
var a = 1, b = 0, L = buf.length, M = 0;
4244
for(var i = 0; i < L;) {
4345
M = Math.min(L-i, 3850)+i;
4446
for(;i<M;i++) {
@@ -51,31 +53,31 @@ function adler32_buf(buf) {
5153
return ((b%65521) << 16) | (a%65521);
5254
}
5355

54-
/* much much faster to intertwine utf8 and adler */
5556
function adler32_str(str) {
56-
var a = 1, b = 0, L = str.length, M, c, d;
57+
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
5758
for(var i = 0; i < L;) {
5859
M = Math.min(L-i, 3850);
5960
while(M>0) {
6061
c = str.charCodeAt(i++);
61-
if(c < 0x80) { a += c; b += a; --M; }
62+
if(c < 0x80) { a += c; }
6263
else if(c < 0x800) {
6364
a += 192|((c>>6)&31); b += a; --M;
64-
a += 128|(c&63); b += a; --M;
65+
a += 128|(c&63);
6566
} else if(c >= 0xD800 && c < 0xE000) {
6667
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
6768
a += 240|((c>>8)&7); b += a; --M;
6869
a += 128|((c>>2)&63); b += a; --M;
6970
a += 128|((d>>6)&15)|((c&3)<<4); b += a; --M;
70-
a += 128|(d&63); b += a; --M;
71+
a += 128|(d&63);
7172
} else {
7273
a += 224|((c>>12)&15); b += a; --M;
7374
a += 128|((c>>6)&63); b += a; --M;
74-
a += 128|(c&63); b += a; --M;
75+
a += 128|(c&63);
7576
}
77+
b += a; --M;
7678
}
77-
a %= 65521;
78-
b %= 65521;
79+
a = (15*(a>>>16)+(a&65535));
80+
b = (15*(b>>>16)+(b&65535));
7981
}
8082
return (b << 16) | a;
8183
}

0 commit comments

Comments
 (0)