Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
c672ba9c52 | |||
700ebdb524 | |||
d8b2f48bd9 | |||
7847c20022 | |||
a79d4e4b32 | |||
6fb0427a31 | |||
|
850ab38461 | ||
1c4d6e97fc | |||
e6d6f001c5 | |||
f04791d1e7 | |||
|
860f1ed9e6 | ||
e1e33f9cb6 | |||
dc687f5ed0 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@ node_modules
|
||||
misc/coverage.html
|
||||
ctest/sauce*
|
||||
*.sheetjs
|
||||
*.tgz
|
||||
package-lock.json
|
40
.spelling
Normal file
40
.spelling
Normal file
@ -0,0 +1,40 @@
|
||||
# printj (C) 2016-present SheetJS -- http://sheetjs.com
|
||||
SheetJS
|
||||
printj
|
||||
printf
|
||||
|
||||
# printf-related terms
|
||||
16-bit
|
||||
32-bit
|
||||
52-bit
|
||||
64-bit
|
||||
base-10
|
||||
fmt
|
||||
|
||||
# Third-party
|
||||
AltiVec
|
||||
FreeBSD
|
||||
glibc
|
||||
libc
|
||||
nodejs
|
||||
npm
|
||||
unicode
|
||||
|
||||
# Other terms
|
||||
CommonJS
|
||||
NaN
|
||||
UTF-16
|
||||
accessor
|
||||
bitwise
|
||||
codepages
|
||||
conformant
|
||||
errno
|
||||
falsy
|
||||
runtime
|
||||
trigraphs
|
||||
truthy
|
||||
typeof
|
||||
valueOf
|
||||
variadic
|
||||
whitespace
|
||||
|
191
LICENSE
191
LICENSE
@ -1,4 +1,192 @@
|
||||
Copyright (C) 2016-present SheetJS
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright (C) 2016-present SheetJS LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@ -11,4 +199,3 @@ Copyright (C) 2016-present SheetJS
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
45
Makefile
45
Makefile
@ -9,8 +9,16 @@ ULIB=$(shell echo $(LIB) | tr a-z A-Z)
|
||||
DEPS=$(sort $(wildcard bits/*.js))
|
||||
TARGET=$(LIB).js
|
||||
FLOWTARGET=$(LIB).flow.js
|
||||
MJSTARGET=$(LIB).mjs
|
||||
FLOWTGTS=$(TARGET) $(AUXTARGETS)
|
||||
CLOSURE=/usr/local/lib/node_modules/google-closure-compiler/compiler.jar
|
||||
UGLIFY="./node_modules/@sheetjs/uglify-js/bin/uglifyjs"
|
||||
JSCS=./node_modules/.bin/jscs
|
||||
JSHINT=./node_modules/.bin/jshint
|
||||
MOCHA=./node_modules/.bin/mocha
|
||||
ESLINT=./node_modules/.bin/eslint
|
||||
ALEX=./node_modules/.bin/alex
|
||||
MDSPELL=./node_modules/.bin/mdspell
|
||||
|
||||
## Main Targets
|
||||
|
||||
@ -26,6 +34,7 @@ $(FLOWTGTS): %.js : %.flow.js
|
||||
|
||||
$(FLOWTARGET): $(DEPS) lib
|
||||
cp lib/$(REQS).js $(FLOWTARGET)
|
||||
cp lib/$(REQS).mjs $(MJSTARGET)
|
||||
|
||||
bits/01_version.js: package.json
|
||||
echo "$(ULIB).version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@
|
||||
@ -35,18 +44,26 @@ clean: clean-stress ## Remove targets and build artifacts
|
||||
@OUTDIR=$(PWD)/lib make -C bits clean
|
||||
rm -f $(TARGET) $(FLOWTARGET)
|
||||
|
||||
.PHONY: init
|
||||
init: ## Initial setup for development
|
||||
npm i
|
||||
|
||||
.PHONY: dist
|
||||
dist: $(TARGET) ## Prepare JS files for distribution
|
||||
cp $(TARGET) dist/
|
||||
cp LICENSE dist/
|
||||
uglifyjs $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)"
|
||||
$(UGLIFY) $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)"
|
||||
misc/strip_sourcemap.sh dist/$(LIB).min.js
|
||||
|
||||
## Testing
|
||||
|
||||
.PHONY: test mocha
|
||||
test mocha: test.js $(TARGET) ## Run test suite
|
||||
mocha -R spec -t 20000
|
||||
$(MOCHA) -R spec -t 20000
|
||||
|
||||
.PHONY: test-deno
|
||||
test-deno: test.ts ## Run Deno test suite
|
||||
deno test --check --allow-env --allow-read --allow-write --config misc/test.deno.jsonc $<
|
||||
|
||||
.PHONY: ctest
|
||||
ctest: ## Build browser test (into ctest/ subdirectory)
|
||||
@ -56,7 +73,7 @@ ctest: ## Build browser test (into ctest/ subdirectory)
|
||||
|
||||
.PHONY: ctestserv
|
||||
ctestserv: ## Start a test server on port 8000
|
||||
@cd ctest && python -mSimpleHTTPServer
|
||||
@cd ctest && python -mSimpleHTTPServer || python3 -mhttp.server || npx -y http-server -p 8000 .
|
||||
|
||||
.PHONY: stress
|
||||
stress: ## Run stress tests
|
||||
@ -71,20 +88,20 @@ clean-stress: ## Remove stress tests
|
||||
## Code Checking
|
||||
|
||||
.PHONY: fullint
|
||||
fullint: lint old-lint tslint flow mdlint ## Run all checks
|
||||
fullint: lint old-lint tslint mdlint ## Run all checks
|
||||
|
||||
.PHONY: lint
|
||||
lint: $(TARGET) ## Run eslint checks
|
||||
@eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json bower.json
|
||||
@$(ESLINT) --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json
|
||||
if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi
|
||||
|
||||
.PHONY: old-lint
|
||||
old-lint: $(TARGET) ## Run jshint and jscs checks
|
||||
@jshint --show-non-errors $(TARGET) $(AUXTARGETS)
|
||||
@jshint --show-non-errors $(CMDS)
|
||||
@jshint --show-non-errors package.json
|
||||
@jshint --show-non-errors --extract=always $(HTMLLINT)
|
||||
@jscs lib/*.js
|
||||
@$(JSHINT) --show-non-errors $(TARGET) $(AUXTARGETS)
|
||||
@$(JSHINT) --show-non-errors $(CMDS)
|
||||
@$(JSHINT) --show-non-errors package.json
|
||||
@$(JSHINT) --show-non-errors --extract=always $(HTMLLINT)
|
||||
@$(JSCS) lib/*.js
|
||||
if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi
|
||||
|
||||
.PHONY: tslint
|
||||
@ -100,17 +117,17 @@ flow: lint ## Run flow checker
|
||||
cov: misc/coverage.html ## Run coverage test
|
||||
|
||||
misc/coverage.html: $(TARGET) test.js
|
||||
mocha --require blanket -R html-cov -t 20000 > $@
|
||||
$(MOCHA) --require blanket -R html-cov -t 20000 > $@
|
||||
|
||||
.PHONY: coveralls
|
||||
coveralls: ## Coverage Test + Send to coveralls.io
|
||||
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js
|
||||
$(MOCHA) --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js
|
||||
|
||||
MDLINT=README.md
|
||||
.PHONY: mdlint
|
||||
mdlint: $(MDLINT) ## Check markdown documents
|
||||
alex $^
|
||||
mdspell -a -n -x -r --en-us $^
|
||||
$(ALEX) $^
|
||||
$(MDSPELL) -a -n -x -r --en-us $^
|
||||
|
||||
.PHONY: help
|
||||
help:
|
||||
|
204
README.md
204
README.md
@ -7,22 +7,85 @@ compliance, performance and IE6+ support.
|
||||
PRINTJ.sprintf("Hello %s!", "World");
|
||||
```
|
||||
|
||||
A self-contained specification of the printf format string is included below in [this README](#printf-format-string-specification), as well as a summary of the
|
||||
A self-contained specification of the printf format string is included below in
|
||||
[this README](#printf-format-string-specification), as well as a summary of the
|
||||
[support against various printf implementations](#support-summary)
|
||||
|
||||
|
||||
## Table of Contents
|
||||
|
||||
<details>
|
||||
<summary><b>Table of Contents</b> (click to show)</summary>
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
* [Installation](#installation)
|
||||
+ [ES Module Support](#es-module-support)
|
||||
* [Usage](#usage)
|
||||
* [Testing](#testing)
|
||||
* [License](#license)
|
||||
* [Badges](#badges)
|
||||
- [printf format string specification](#printf-format-string-specification)
|
||||
+ [Original C Interface](#original-c-interface)
|
||||
+ [JS and C strings](#js-and-c-strings)
|
||||
+ [JS Interface](#js-interface)
|
||||
* [Specifier heritage and regular expression](#specifier-heritage-and-regular-expression)
|
||||
* [Conversion Specifier Quick Reference Table](#conversion-specifier-quick-reference-table)
|
||||
* [Parameter Selection](#parameter-selection)
|
||||
* [Dynamic Specifiers](#dynamic-specifiers)
|
||||
- [C Data Model](#c-data-model)
|
||||
+ [Integer Types](#integer-types)
|
||||
+ [Character and String Types](#character-and-string-types)
|
||||
+ [Floating Point Number Types](#floating-point-number-types)
|
||||
* [Implementation](#implementation)
|
||||
- [Integer Conversions](#integer-conversions)
|
||||
* [Restricting Integer Values](#restricting-integer-values)
|
||||
* [Length Specifiers for Integer Conversions](#length-specifiers-for-integer-conversions)
|
||||
* [Rendering Unsigned Integers in Base 10 ("u" and "U" conversions)](#rendering-unsigned-integers-in-base-10-u-and-u-conversions)
|
||||
* [Rendering Unsigned Integers in Base 8 ("o" and "O" conversions)](#rendering-unsigned-integers-in-base-8-o-and-o-conversions)
|
||||
* [Rendering Unsigned Integers in Base 16 ("x" and "X" conversions)](#rendering-unsigned-integers-in-base-16-x-and-x-conversions)
|
||||
* [Rendering Signed Integers in Base 10 ("d" "i" and "D" conversions)](#rendering-signed-integers-in-base-10-d-i-and-d-conversions)
|
||||
- [Floating Point Conversions](#floating-point-conversions)
|
||||
* [Infinity, NaN, and Negative Zero](#infinity-nan-and-negative-zero)
|
||||
* [Exponential Form ("e" and "E" conversions)](#exponential-form-e-and-e-conversions)
|
||||
* [Standard Form ("f" and "F" conversions)](#standard-form-f-and-f-conversions)
|
||||
* [Value-dependent Form ("g" and "G" conversions)](#value-dependent-form-g-and-g-conversions)
|
||||
* [Hex-Mantissa Decimal-Binary-Exponent Form ("a" and "A" conversions)](#hex-mantissa-decimal-binary-exponent-form-a-and-a-conversions)
|
||||
- [Character Conversions](#character-conversions)
|
||||
* [Rendering Strings ("s" and "S" conversions)](#rendering-strings-s-and-s-conversions)
|
||||
* [Rendering Characters ("c" and "C" conversions)](#rendering-characters-c-and-c-conversions)
|
||||
- [Non-Numeric Conversions](#non-numeric-conversions)
|
||||
* [The literal "%" symbol ("%" conversion)](#the-literal-%25-symbol-%25-conversion)
|
||||
* [Interpreting and Rendering Pointers ("p" conversion)](#interpreting-and-rendering-pointers-p-conversion)
|
||||
* [Extracting length of a partial conversion ("n" conversion)](#extracting-length-of-a-partial-conversion-n-conversion)
|
||||
* [Error messages ("m" conversion)](#error-messages-m-conversion)
|
||||
- [Extensions](#extensions)
|
||||
* [Rendering Boolean Values ("y" and "Y" conversions)](#rendering-boolean-values-y-and-y-conversions)
|
||||
* [Rendering JSON ("J" conversion)](#rendering-json-j-conversion)
|
||||
* [JS typeof and valueOf ("T" and "V" conversion)](#js-typeof-and-valueof-t-and-v-conversion)
|
||||
* [Rendering Unsigned Integers in Base 2 ("b" and "B" conversions)](#rendering-unsigned-integers-in-base-2-b-and-b-conversions)
|
||||
- [Miscellaneous Notes](#miscellaneous-notes)
|
||||
* [Format Characters](#format-characters)
|
||||
* [JS and C strings](#js-and-c-strings-1)
|
||||
* [Browser Deviations](#browser-deviations)
|
||||
* [Support Summary](#support-summary)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
</details>
|
||||
|
||||
## Installation
|
||||
|
||||
With [npm](https://www.npmjs.org/package/printj):
|
||||
A NodeJS package is available on the SheetJS CDN:
|
||||
|
||||
```bash
|
||||
$ npm install printj
|
||||
$ npm install --save https://cdn.sheetjs.com/printj-1.3.2/printj-1.3.2.tgz
|
||||
```
|
||||
|
||||
In the browser:
|
||||
|
||||
```html
|
||||
<script src="printj.js"></script>
|
||||
<script src="https://cdn.sheetjs.com/printj-1.3.2/package/dist/printj.js"></script>
|
||||
```
|
||||
|
||||
The browser exposes a variable `PRINTJ`
|
||||
@ -30,9 +93,40 @@ The browser exposes a variable `PRINTJ`
|
||||
When installed globally, npm installs a script `printj` that renders the format
|
||||
string with the given arguments. Running the script with `-h` displays help.
|
||||
|
||||
The script will manipulate `module.exports` if available (e.g. in a CommonJS
|
||||
`require` context). This is not always desirable. To prevent the behavior,
|
||||
define `DO_NOT_EXPORT_PRINTJ`
|
||||
The script will manipulate `module.exports` if available. This is not always
|
||||
desirable. To prevent the behavior, define `DO_NOT_EXPORT_PRINTJ`
|
||||
|
||||
### ES Module Support
|
||||
|
||||
The bundle ships with a `printj.mjs` script that acts as an ES Module.
|
||||
|
||||
**NodeJS**
|
||||
|
||||
NodeJS 12+ support ES modules. The default import uses the CommonJS script:
|
||||
|
||||
```js
|
||||
import PRINTJ from "printj";
|
||||
```
|
||||
|
||||
It is possible to use the ESM powered script referencing `printj.mjs` directly:
|
||||
|
||||
```js
|
||||
import * as PRINTJ from "printj/printj.mjs"; // pull all exports
|
||||
import { sprintf } from "printj/printj.mjs"; // pull `sprintf`
|
||||
```
|
||||
|
||||
**Browser Module Support**
|
||||
|
||||
Chrome 61+ and Safari 11+ support module imports in the web browser. The `.mjs`
|
||||
script can be imported from a `script type=module` block:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import { sprintf } from './printj.mjs';
|
||||
console.log(sprintf("%02hhx", 123));
|
||||
</script>
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
@ -95,18 +189,6 @@ granted by the Apache 2.0 license are reserved by the Original Author.
|
||||
|
||||
[](https://saucelabs.com/u/printj)
|
||||
|
||||
[](https://travis-ci.org/SheetJS/printj)
|
||||
|
||||
[](https://coveralls.io/r/SheetJS/printj?branch=master)
|
||||
|
||||
[](https://npmjs.org/package/printj)
|
||||
|
||||
[](https://david-dm.org/sheetjs/printj)
|
||||
|
||||
[](https://ghit.me/repo/sheetjs/printj)
|
||||
|
||||
[](https://github.com/SheetJS/printj)
|
||||
|
||||
# printf format string specification
|
||||
|
||||
The `printf` family of functions attempt to generate and output a string of
|
||||
@ -125,8 +207,8 @@ but have different interfaces reflecting different input and output behaviors.
|
||||
Some functions have wide variants that use wide `wchar_t *` strings rather than
|
||||
normal C `char *`. The following variants are required by the POSIX spec:
|
||||
|
||||
| function | max length | output destination | vintage | wide ver |
|
||||
|------------|------------|-----------------------|---------|------------|
|
||||
| function | max length | output destination | vintage | wide form |
|
||||
|:-----------|:-----------|:----------------------|:--------|:-----------|
|
||||
| `printf` | unbounded | standard output | K&R | `wprintf` |
|
||||
| `fprintf` | unbounded | stream (`FILE *`) | K&R | `fwprintf` |
|
||||
| `sprintf` | unbounded | string (`char *`) | K&R | `swprintf` |
|
||||
@ -185,7 +267,7 @@ various string functions are included at the end of the document.
|
||||
|
||||
## Specifier heritage and regular expression
|
||||
|
||||
Note: The regular expressions follow perl `/x` style. Whitespace characters
|
||||
Note: The regular expressions follow Perl `/x` style. Whitespace characters
|
||||
outside of character classes are ignored. `#` is a comment character and every
|
||||
character until the end of the line is ignored. To convert to a standard regex:
|
||||
|
||||
@ -298,7 +380,7 @@ This implementation explicitly does not support certain non-standard extensions:
|
||||
| `i` | integral | cast to C `int`, standard form decimal (alias of `d`) |
|
||||
| `J` | extended | prints objects using JSON or `util.inspect` |
|
||||
| `m` | misc | prints info about Error objects (JS equivalent of `errno`) |
|
||||
| `n` | misc | do not print! stores number of chars written to arg `.len` |
|
||||
| `n` | misc | do not print! store number of chars written to `.len` field |
|
||||
| `o` | integral | cast to C `unsigned int`, standard form octal |
|
||||
| `O` | integral | cast to C `unsigned long`, standard form octal |
|
||||
| `p` | misc | print `"l"` field of object (fake pointer) |
|
||||
@ -323,8 +405,8 @@ printf("Count to 3: %d %d %d", 1, 2, 3); // Count to 3: 1 2 3
|
||||
```
|
||||
|
||||
POSIX `printf` permits explicit argument selection, bypassing the standard
|
||||
behavior of using the arguments in order. To select the `n`-th argument, use
|
||||
`n$` immediately after the `%` token to select an argument for the conversion:
|
||||
behavior of consuming arguments in order. To specify the argument at position
|
||||
`n`, use `n$` immediately after the `%` token:
|
||||
|
||||
```C
|
||||
printf("%d %d %d", 1, 2, 3); // 1 2 3 (implicit order 1, 2, 3 )
|
||||
@ -463,11 +545,11 @@ Numerous "C data models", specifying the bit/byte sizes of the various types,
|
||||
have been and continue to be used. For example, OSX and other modern 64-bit
|
||||
UNIX flavors use the "LP64" C data model. 64-bit Windows currently uses the
|
||||
"LLP64" model. 32-bit systems generally use the "ILP32" model. The 8-bit byte
|
||||
sizes for the various types under the various models are defined in ctypes.json
|
||||
sizes for the data types under the various models are defined in `ctypes.json`
|
||||
in the `Models` object as per the following table:
|
||||
|
||||
| type | ctypes.json | LP64 | ILP32 | LLP64 |
|
||||
|-------------|-------------|-----:|------:|------:|
|
||||
| type | JSON key | LP64 | ILP32 | LLP64 |
|
||||
|:------------|:------------|-----:|------:|------:|
|
||||
| `char` | `char` | 1 | 1 | 1 |
|
||||
| `short` | `short` | 2 | 2 | 2 |
|
||||
| `int` | `int` | 4 | 4 | 4 |
|
||||
@ -535,7 +617,7 @@ lengths exceeding 32 bits, `Math.round` is appropriate.
|
||||
|------|---------------------------|-------------------------------------------|
|
||||
| 8 | `V & 0xFF` | `V &= 0xFF; if(V > 0x7F) V-= 0x100` |
|
||||
| 16 | `V & 0xFFFF` | `V &= 0xFFFF; if(V > 0x7FFF) V-= 0x10000` |
|
||||
| 32 | `V >>> 0` | `V | 0` |
|
||||
| 32 | `V >>> 0` | `V \| 0` |
|
||||
| 64 | `Math.abs(Math.round(V))` | `Math.round(V)` |
|
||||
|
||||
## Length Specifiers for Integer Conversions
|
||||
@ -555,16 +637,16 @@ is signed or unsigned according to the conversion specifier. If a length is
|
||||
specified, it overrides the implied length of the conversion. The following
|
||||
table describes the behavior of this implementation:
|
||||
|
||||
| implied C type | ctypes.json | length | conv default |
|
||||
|:------------------------------------|:------------|:------:|:-------------|
|
||||
| `int` or `unsigned int` | `int` | (none) | d i o u x X |
|
||||
| `char` or `unsigned char` | `char` | hh |
|
||||
| `short` or `unsigned short` | `short` | h |
|
||||
| `long` or `unsigned long` | `long` | l | D U O |
|
||||
| `long long` or `unsigned long long` | `longlong` | L ll q |
|
||||
| `intmax_t` or `uintmax_t` | `intmax_t` | j |
|
||||
| `size_t` or `ssize_t` | `size_t` | z Z |
|
||||
| `ptrdiff_t` or unsigned variant | `ptrdiff_t` | t |
|
||||
| implied C type | JSON key | length | conversion default |
|
||||
|:-----------------------------|:------------|:--------:|:-------------------|
|
||||
| `[unsigned] int` | `int` | (none) | `d i o u x X` |
|
||||
| `[unsigned] char` | `char` | `hh` |
|
||||
| `[unsigned] short` | `short` | `h` |
|
||||
| `[unsigned] long` | `long` | `l` | `D U O` |
|
||||
| `[unsigned] long long` | `longlong` | `L ll q` |
|
||||
| `intmax_t` or `uintmax_t` | `intmax_t` | `j` |
|
||||
| `size_t` or `ssize_t` | `size_t` | `z Z` |
|
||||
| `ptrdiff_t` or unsigned form | `ptrdiff_t` | `t` |
|
||||
|
||||
## Rendering Unsigned Integers in Base 10 ("u" and "U" conversions)
|
||||
|
||||
@ -615,7 +697,7 @@ JS recognizes a few special IEEE754 values, as described in the following table:
|
||||
|------------:|:--------------|:-----------------------------------------------|
|
||||
| `Infinity` | `1./0.` | Positive limiting value `lim{x->0+} 1/x` |
|
||||
| `-Infinity` | `-1./0.` | Negative limiting value `lim{x->0+} -1/x` |
|
||||
| `NaN` | `0./0.` | Placeholder for "not-a-number" e.g. `0./0.` |
|
||||
| `NaN` | `0./0.` | Placeholder for "not-a-number" such as `0./0.` |
|
||||
| `-0.` | `-1/Infinity` | Negative limiting value `lim{x->0-} x` |
|
||||
|
||||
JS `Number` methods render different strings from the POSIX spec:
|
||||
@ -625,7 +707,7 @@ JS `Number` methods render different strings from the POSIX spec:
|
||||
| `Infinity` | `"inf" "INF"` or `"infinity" "INFINITY"` | `"Infinity"` |
|
||||
| `-Infinity` | `"-inf" "-INF"` or `"-infinity" "-INFINITY"` | `"-Infinity"` |
|
||||
| `NaN` | `"[-]nan" "[-]NAN"` w/opt parenthesized chars | `"NaN"` |
|
||||
| `-0.` | uses negative sign (e.g. `"-0"` under `"%f"`) | same as `+0.` |
|
||||
| `-0.` | uses negative sign (`"-0"` under `"%f"`) | same as `+0.` |
|
||||
|
||||
This implementation performs the required adjustments.
|
||||
|
||||
@ -634,10 +716,10 @@ This implementation performs the required adjustments.
|
||||
Aside from the special cases discussed above, JS `num.toExponential(prec)`
|
||||
differs from POSIX `printf("%1$.*2$e", num, prec)` in the exponent field: JS
|
||||
writes exponents with the fewest digits (POSIX requires 2+ digits). This is
|
||||
easily fixed by inspecting the output string and inserting a "0" when needed.
|
||||
addressed by inspecting the output string and inserting a "0" when needed.
|
||||
|
||||
The optional `#` flag forces the decimal point to appear when precision is 0.
|
||||
This is also easily corrected by adding a decimal point just before the "e".
|
||||
This is also fixed by adding a decimal point just before the "e".
|
||||
|
||||
## Standard Form ("f" and "F" conversions)
|
||||
|
||||
@ -656,12 +738,12 @@ The final form (exponential or standard) is determined based on the value. The
|
||||
threshold is different from the JS `toString` / `toPrecision` thresholds and
|
||||
depends on the specified precision as well as the base-10 exponent:
|
||||
|
||||
| Value | `"%.3g"` | `toPrecision(3)` |
|
||||
|----------:|:-----------|:-----------------|
|
||||
| 1.2345e-4 | `0.000123` | `0.000123` |
|
||||
| 1.2345e-5 | `1.23e-05` | `0.0000123` |
|
||||
| 1.2345e-6 | `1.23e-06` | `0.00000123` |
|
||||
| 1.2345e-7 | `1.23e-07` | `1.23e-7` |
|
||||
| Value | `"%.3g"` | `toPrecision(3)` |
|
||||
|------------:|:-----------|:-----------------|
|
||||
| `1.2345e-4` | `0.000123` | `0.000123` |
|
||||
| `1.2345e-5` | `1.23e-05` | `0.0000123` |
|
||||
| `1.2345e-6` | `1.23e-06` | `0.00000123` |
|
||||
| `1.2345e-7` | `1.23e-07` | `1.23e-7` |
|
||||
|
||||
According to JS spec, `toPrecision` uses standard form when `precision > E` and
|
||||
`E >= -6`. For printf standard form is used when `precision > E` and `E >= -4`.
|
||||
@ -673,24 +755,24 @@ the exponent expression, and radix of the exponent expression. The standard
|
||||
exponential form uses decimal for all three parts. For base 16, there are quite
|
||||
a few reasonable combinations. Consider the value `1.234567e-80`:
|
||||
|
||||
| Mant | Exp Base | Radix-10 (sigil `";"`) | Radix-16 (sigil `";"`) |
|
||||
|:----:|:--------:|:-----------------------|:-----------------------|
|
||||
| 10 | 10 | `1.234567;-80` | `1.234567;-50` |
|
||||
| 16 | 10 | `1.3c0c9539b8887;-80` | `1.3c0c9539b8887;-50` |
|
||||
| 16 | 16 | `5.daf8c8f5f4104;-67` | `5.daf8c8f5f4104;-43` |
|
||||
| 16 | 4 | `1.76be323d7d041;-133` | `1.76be323d7d041;-85` |
|
||||
| 16 | 2 | `1.76be323d7d041;-266` | `1.76be323d7d041;-10a` |
|
||||
| Mantissa | Exp Base | Radix 10 (sigil `";"`) | Radix 16 (sigil `";"`) |
|
||||
|:--------:|:--------:|:-----------------------|:-----------------------|
|
||||
| 10 | 10 | `1.234567;-80` | `1.234567;-50` |
|
||||
| 16 | 10 | `1.3c0c9539b8887;-80` | `1.3c0c9539b8887;-50` |
|
||||
| 16 | 16 | `5.daf8c8f5f4104;-67` | `5.daf8c8f5f4104;-43` |
|
||||
| 16 | 4 | `1.76be323d7d041;-133` | `1.76be323d7d041;-85` |
|
||||
| 16 | 2 | `1.76be323d7d041;-266` | `1.76be323d7d041;-10a` |
|
||||
|
||||
POSIX `"%a"` uses a hex mantissa (16), decimal exponent radix (10), and binary
|
||||
exponent base (2). The general normalized form requires that the integral part
|
||||
of the mantissa to exceed 0 and not to exceed `exponent base - 1` except in the
|
||||
special case of `0`. The sigil is `p` and exponent sign is always used.
|
||||
|
||||
JS `num.toString(radix)` is implementation-dependent for valid non-10 radices
|
||||
JS `num.toString(radix)` is implementation-dependent for radices other than 10
|
||||
(`2-9, 11-36`). IE uses hex-mantissa decimal-hex-exponent form when the
|
||||
absolute value of the base-2 exponent exceeds 60. Otherwise, IE uses an exact
|
||||
standard hexadecimal form. Chrome, Safari and other browsers always use the
|
||||
exact standard hexadecimal form. Both forms are easily converted to `"%a"` by
|
||||
exact standard hexadecimal form. Both forms are converted to `"%a"` by
|
||||
calculating and dividing by the appropriate power of 2.
|
||||
|
||||
For each non-zero normal floating point value, there are 4 acceptable strings
|
||||
@ -840,7 +922,7 @@ In all forms other than `"%m"`, an argument will be processed as follows:
|
||||
- If the `errno` field is set, emit "Error number " followed by the errno
|
||||
- Otherwise emit "Error " followed by the error interpreted as a String
|
||||
|
||||
```
|
||||
```js
|
||||
var x = new Error("sheetjs");
|
||||
x.errno = 69; x.toString = function() { return "SHEETJS"; };
|
||||
printf("|%#m|", x); // |sheetjs|
|
||||
@ -902,7 +984,7 @@ Width, precision and other flags are ignored.
|
||||
Under the "T" conversion, the result of `typeof arg` is rendered. If the `#`
|
||||
flag is specified, the type is derived from `Object.prototype.toString`:
|
||||
|
||||
```
|
||||
```js
|
||||
printf("%1$T %1$#T", 1); // 'number Number'
|
||||
printf("%1$T %1$#T", 'foo'); // 'string String'
|
||||
printf("%1$T %1$#T", [1,2,3]); // 'object Array'
|
||||
@ -912,7 +994,7 @@ printf("%1$T %1$#T", undefined); // 'undefined Undefined'
|
||||
|
||||
Under the "V" conversion, the result of `arg.valueOf()` is rendered:
|
||||
|
||||
```
|
||||
```js
|
||||
var _f = function() { return "f"; };
|
||||
var _3 = function() { return 3; };
|
||||
printf("%1$d %1$s %1$V", {toString:_f}); // '0 f f'
|
||||
|
@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
/* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */
|
||||
/* printj.js (C) 2016-present SheetJS -- https://sheetjs.com */
|
||||
/* eslint-env node */
|
||||
/* vim: set ts=2 ft=javascript: */
|
||||
/*jshint node:true, evil:true */
|
||||
require("../shim");
|
||||
var X = require("../"), argv = process.argv;
|
||||
|
||||
function help() {
|
||||
@ -27,8 +28,7 @@ function help() {
|
||||
" $ printj '|%s is %s|' s:1+2 e:1+2 # |1+2 is 3|",
|
||||
" $ printj '|%c %c|' s:69 n:69 # |6 E|",
|
||||
"",
|
||||
"Support email: dev@sheetjs.com",
|
||||
"Web Demo: http://oss.sheetjs.com/printj/"
|
||||
"Web Demo: https://oss.sheetjs.com/printj/"
|
||||
].forEach(function(l) { console.log(l); });
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,6 +4,11 @@
|
||||
/*exported PRINTJ */
|
||||
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
|
||||
/*:: declare function define(cb:()=>any):void; */
|
||||
#ifdef USE_ESM
|
||||
var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/;
|
||||
#include "01_version.js"
|
||||
export const version = PRINTJ.version;
|
||||
#else
|
||||
var PRINTJ/*:PRINTJModule*/;
|
||||
(function (factory/*:(a:any)=>void*/)/*:void*/ {
|
||||
/*jshint ignore:start */
|
||||
@ -27,3 +32,4 @@ var PRINTJ/*:PRINTJModule*/;
|
||||
/*jshint ignore:end */
|
||||
}(function(PRINTJ/*:PRINTJModule*/) {
|
||||
#include "01_version.js"
|
||||
#endif
|
||||
|
@ -1 +1 @@
|
||||
PRINTJ.version = '1.1.2';
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
@ -4,8 +4,11 @@
|
||||
#define GETCHAR c = fmt.charCodeAt(i);
|
||||
#endif
|
||||
|
||||
var tcache = {};
|
||||
|
||||
#define DRAIN(idx) if(start < idx) out.push(["L", fmt.substring(start, idx)]);
|
||||
function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out/*:ParsedFmt*/ = [];
|
||||
var start/*:number*/ = 0;
|
||||
|
||||
@ -16,7 +19,7 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
DRAIN(m.index)
|
||||
start = m.index + m[0].length;
|
||||
if(m[0] === "%%") out.push(["%","%"]);
|
||||
else out.push([m[6], m[0], m[1], m[2], m[3], m[4], m[5]]);
|
||||
else out.push([m[6], m[0]||"", m[1]||"", m[2]||"", m[3]||"", m[4]||"", m[5]||""]);
|
||||
}
|
||||
|
||||
#else /* NOT USE_REGEX */
|
||||
@ -74,5 +77,5 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
#endif /* USE_REGEX */
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return out;
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
@ -1,12 +1,5 @@
|
||||
#define isnan isNaN
|
||||
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
|
||||
var padstr/*:{[s:string]:string}*/ = {
|
||||
" ": " ",
|
||||
"0": "000000000000000000000000000000000",
|
||||
"7": "777777777777777777777777777777777",
|
||||
"f": "fffffffffffffffffffffffffffffffff"
|
||||
};
|
||||
#define PAD_(x,c) (x >= 0 ? padstr[c].substr(0,x) : "")
|
||||
#define PAD_(x,c) (x >= 0 ? c.repeat(x) : "")
|
||||
#ifdef DO_NOT_INLINE
|
||||
function pads(x/*:number*/, c/*:string*/)/*:string*/ { return PAD_(x,c); }
|
||||
#define PADS(x,c) pads(x,c)
|
||||
|
@ -1,13 +1,18 @@
|
||||
#include "30_ctypes.js"
|
||||
#include "40_macros.js"
|
||||
#ifdef USE_ESM
|
||||
var u_inspect/*:(o:any)=>string*/ = JSON.stringify;
|
||||
#else
|
||||
/*:: var util = require('util'); */
|
||||
/*global process:true, util:true, require:true */
|
||||
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
|
||||
var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
|
||||
|
||||
#endif
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var o/*:Array<string>*/ = [];
|
||||
//var o/*:Array<string>*/ = [];
|
||||
var o = "";
|
||||
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
|
||||
var Vnum/*:number*/ = 0;
|
||||
var pad/*:string*/ = "";
|
||||
@ -15,24 +20,24 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o.push(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o.push("%"); continue; }
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O/*:string*/ = "";
|
||||
var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = SIZEOF_INT, sign/*:boolean*/ = false;
|
||||
|
||||
/* flags */
|
||||
var flags/*:string*/ = m[IDX_FLAGS]||"";
|
||||
var flags/*:string*/ = m[IDX_FLAGS]/*||""*/;
|
||||
var alt/*:boolean*/ = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[IDX_POS]) argidx = parseInt(m[IDX_POS])-1;
|
||||
if(m[IDX_POS]) argidx = parseInt(m[IDX_POS], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; }
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
#define GRAB_INT(NAME, IDX, DFL) \
|
||||
var NAME/*:number*/ = DFL; \
|
||||
if(m[IDX] != null && m[IDX].length > 0) { \
|
||||
if(/*m[IDX] != null &&*/ m[IDX].length > 0) { \
|
||||
if(m[IDX].charAt(0) !== '*') NAME = parseInt(m[IDX], 10); \
|
||||
else if(m[IDX].length === 1) NAME = args[idx++]; \
|
||||
else NAME = args[parseInt(m[IDX].substr(1), 10)-1]; \
|
||||
@ -51,7 +56,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var arg/*:any*/ = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len/*:string*/ = m[IDX_LEN] || "";
|
||||
var len/*:string*/ = m[IDX_LEN]/* || ""*/;
|
||||
|
||||
#define INT isnum = -1
|
||||
#define SGN(s) sign = s
|
||||
@ -78,7 +83,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
#include "70_float.js"
|
||||
}
|
||||
|
||||
o.push(O);
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o.join("");
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
@ -11,7 +11,8 @@
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
@ -35,7 +36,7 @@
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
PREC_STR(O, prec)
|
||||
WIDTH(O, width, flags)
|
||||
|
@ -2,3 +2,4 @@ PRINTJ.sprintf = sprintf;
|
||||
PRINTJ.vsprintf = vsprintf;
|
||||
PRINTJ._doit = doit;
|
||||
PRINTJ._tokenize = tokenize;
|
||||
PRINTJ.set_inspect = set_inspect;
|
1
bits/99_esmfoot.js
Normal file
1
bits/99_esmfoot.js
Normal file
@ -0,0 +1 @@
|
||||
export { sprintf, vsprintf, set_inspect };
|
@ -2,8 +2,8 @@ ifndef OUTDIR
|
||||
OUTDIR=$(PWD)/lib
|
||||
endif
|
||||
|
||||
JSFILES=$(wildcard *.js)
|
||||
LIBS=$(filter-out $(wildcard [0-9]*_*.js),$(wildcard *.js))
|
||||
JSFILES=$(wildcard *.js) $(wildcard *.mjs)
|
||||
LIBS=$(filter-out $(wildcard [0-9]*_*.js),$(JSFILES))
|
||||
|
||||
OUTLIBS=$(patsubst %,$(OUTDIR)/%,$(LIBS))
|
||||
|
||||
|
@ -10,6 +10,6 @@ declare class PRINTJModule {
|
||||
_doit:(t:ParsedFmt, args:Args)=>string;
|
||||
_tokenize:(fmt:string)=>ParsedFmt;
|
||||
};
|
||||
declare module "./" { declare var exports:PRINTJModule };
|
||||
declare module "../" { declare var exports:PRINTJModule };
|
||||
declare module "./" { declare module.exports:PRINTJModule };
|
||||
declare module "../" { declare module.exports:PRINTJModule };
|
||||
*/
|
||||
|
8
bits/loop_code.mjs
Normal file
8
bits/loop_code.mjs
Normal file
@ -0,0 +1,8 @@
|
||||
#define USE_ESM
|
||||
#include "00_header.js"
|
||||
#define USE_LOOP
|
||||
#define USE_CODE
|
||||
#include "10_tokenize.js"
|
||||
#include "50_doit.js"
|
||||
#include "80_wrapper.js"
|
||||
#include "99_esmfoot.js"
|
@ -25,9 +25,12 @@ var PRINTJ;
|
||||
/*jshint ignore:end */
|
||||
}(function(PRINTJ) {
|
||||
|
||||
PRINTJ.version = '1.1.2';
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt) {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out = [];
|
||||
var start = 0;
|
||||
|
||||
@ -146,23 +149,18 @@ function tokenize(fmt) {
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return out;
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
|
||||
var padstr = {
|
||||
" ": " ",
|
||||
"0": "000000000000000000000000000000000",
|
||||
"7": "777777777777777777777777777777777",
|
||||
"f": "fffffffffffffffffffffffffffffffff"
|
||||
};
|
||||
|
||||
/*global process:true, util:true, require:true */
|
||||
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
|
||||
var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t, args) {
|
||||
var o = [];
|
||||
//var o = [];
|
||||
var o = "";
|
||||
var argidx = 0, idx = 0;
|
||||
var Vnum = 0;
|
||||
var pad = "";
|
||||
@ -170,26 +168,26 @@ function doit(t, args) {
|
||||
var m = t[i], c = (m[0]).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o.push(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o.push("%"); continue; }
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O = "";
|
||||
var isnum = 0, radix = 10, bytes = 4, sign = false;
|
||||
|
||||
/* flags */
|
||||
var flags = m[3]||"";
|
||||
var flags = m[3]/*||""*/;
|
||||
var alt = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2])-1;
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; }
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
@ -198,7 +196,7 @@ function doit(t, args) {
|
||||
var arg = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len = m[6] || "";
|
||||
var len = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
@ -208,7 +206,7 @@ function doit(t, args) {
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
@ -223,7 +221,7 @@ function doit(t, args) {
|
||||
case "string": O = arg.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
@ -284,7 +282,8 @@ function doit(t, args) {
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
@ -308,10 +307,10 @@ function doit(t, args) {
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
@ -373,16 +372,16 @@ function doit(t, args) {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O;
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O;
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O;
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
@ -403,8 +402,8 @@ function doit(t, args) {
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1);
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
@ -425,10 +424,10 @@ function doit(t, args) {
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : "");
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -436,7 +435,7 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -483,8 +482,8 @@ function doit(t, args) {
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : "");
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
@ -498,7 +497,7 @@ function doit(t, args) {
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;}
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac = O.charCodeAt(0);
|
||||
@ -545,11 +544,11 @@ function doit(t, args) {
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
@ -566,9 +565,9 @@ function doit(t, args) {
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : "");
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -576,16 +575,16 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o.push(O);
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o.join("");
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt, args) { return doit(tokenize(fmt), args); }
|
||||
@ -600,6 +599,6 @@ PRINTJ.sprintf = sprintf;
|
||||
PRINTJ.vsprintf = vsprintf;
|
||||
PRINTJ._doit = doit;
|
||||
PRINTJ._tokenize = tokenize;
|
||||
|
||||
PRINTJ.set_inspect = set_inspect;
|
||||
}));
|
||||
|
||||
|
@ -235,3 +235,42 @@ if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) {
|
||||
return target;
|
||||
};
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
|
||||
if (!String.prototype.repeat) {
|
||||
String.prototype.repeat = function(count) {
|
||||
'use strict';
|
||||
if (this == null) {
|
||||
throw new TypeError('can\'t convert ' + this + ' to object');
|
||||
}
|
||||
var str = '' + this;
|
||||
count = +count;
|
||||
if (count != count) {
|
||||
count = 0;
|
||||
}
|
||||
if (count < 0) {
|
||||
throw new RangeError('repeat count must be non-negative');
|
||||
}
|
||||
if (count == Infinity) {
|
||||
throw new RangeError('repeat count must be less than infinity');
|
||||
}
|
||||
count = Math.floor(count);
|
||||
if (str.length == 0 || count == 0) {
|
||||
return '';
|
||||
}
|
||||
// Ensuring count is a 31-bit integer allows us to heavily optimize the
|
||||
// main part. But anyway, most current (August 2014) browsers can't handle
|
||||
// strings 1 << 28 chars or longer, so:
|
||||
if (str.length * count >= 1 << 28) {
|
||||
throw new RangeError('repeat count must not overflow maximum string size');
|
||||
}
|
||||
var maxCount = str.length * count;
|
||||
count = Math.floor(Math.log(count) / Math.log(2));
|
||||
while (count) {
|
||||
str += str;
|
||||
count--;
|
||||
}
|
||||
str += str.substring(0, maxCount - str.length);
|
||||
return str;
|
||||
};
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
/* vim: set ts=2: */
|
||||
/* eslint-env mocha */
|
||||
/*global assert:true */
|
||||
var X;
|
||||
var IMPLS = {}, IMPLA = [], IMPL = [];
|
||||
if(typeof require !== 'undefined') {
|
||||
assert = require('assert');
|
||||
require('./shim');
|
||||
X=require('./');
|
||||
IMPL = require("./lib/impl.json");
|
||||
IMPL.forEach(function(impl, i) { IMPLS[impl] = IMPLA[i] = require("./lib/" + impl); });
|
||||
@ -171,6 +174,13 @@ describe('special cases', function() {
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", undefined), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", null), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
});
|
||||
if(typeof JSON !== "undefined") it('correctly handles JSON conversion: J', function() {
|
||||
var data = ({a:1});
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
X.set_inspect(function(data) { return "|" + JSON.stringify(data) + "|"; });
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
assert.equal(sprintf("%#J", data), "|" + JSON.stringify(data) + "|");
|
||||
});
|
||||
it('handles dynamic specifiers', function() {
|
||||
assert.equal(sprintf("|%5s|", "sheetjs"), "|sheetjs|");
|
||||
assert.equal(sprintf("|%*s|", 5, "sheetjs"), "|sheetjs|");
|
||||
@ -190,4 +200,7 @@ describe('special cases', function() {
|
||||
assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |");
|
||||
assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|");
|
||||
});
|
||||
it('handles long strings', function() {
|
||||
for(var i = 10; i <= 1000; i+= 10) assert.equal(sprintf("%" + i + "s", "abc").length, i);
|
||||
});
|
||||
});
|
||||
|
191
dist/LICENSE
vendored
191
dist/LICENSE
vendored
@ -1,4 +1,192 @@
|
||||
Copyright (C) 2016-present SheetJS
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright (C) 2016-present SheetJS LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@ -11,4 +199,3 @@ Copyright (C) 2016-present SheetJS
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
91
dist/printj.js
vendored
91
dist/printj.js
vendored
@ -25,9 +25,12 @@ var PRINTJ;
|
||||
/*jshint ignore:end */
|
||||
}(function(PRINTJ) {
|
||||
|
||||
PRINTJ.version = '1.1.2';
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt) {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out = [];
|
||||
var start = 0;
|
||||
|
||||
@ -146,23 +149,18 @@ function tokenize(fmt) {
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return out;
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
|
||||
var padstr = {
|
||||
" ": " ",
|
||||
"0": "000000000000000000000000000000000",
|
||||
"7": "777777777777777777777777777777777",
|
||||
"f": "fffffffffffffffffffffffffffffffff"
|
||||
};
|
||||
|
||||
/*global process:true, util:true, require:true */
|
||||
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
|
||||
var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t, args) {
|
||||
var o = [];
|
||||
//var o = [];
|
||||
var o = "";
|
||||
var argidx = 0, idx = 0;
|
||||
var Vnum = 0;
|
||||
var pad = "";
|
||||
@ -170,26 +168,26 @@ function doit(t, args) {
|
||||
var m = t[i], c = (m[0]).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o.push(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o.push("%"); continue; }
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O = "";
|
||||
var isnum = 0, radix = 10, bytes = 4, sign = false;
|
||||
|
||||
/* flags */
|
||||
var flags = m[3]||"";
|
||||
var flags = m[3]/*||""*/;
|
||||
var alt = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2])-1;
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; }
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
@ -198,7 +196,7 @@ function doit(t, args) {
|
||||
var arg = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len = m[6] || "";
|
||||
var len = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
@ -208,7 +206,7 @@ function doit(t, args) {
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
@ -223,7 +221,7 @@ function doit(t, args) {
|
||||
case "string": O = arg.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
@ -284,7 +282,8 @@ function doit(t, args) {
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
@ -308,10 +307,10 @@ function doit(t, args) {
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
@ -373,16 +372,16 @@ function doit(t, args) {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O;
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O;
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O;
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
@ -403,8 +402,8 @@ function doit(t, args) {
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1);
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
@ -425,10 +424,10 @@ function doit(t, args) {
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : "");
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -436,7 +435,7 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -483,8 +482,8 @@ function doit(t, args) {
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : "");
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
@ -498,7 +497,7 @@ function doit(t, args) {
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;}
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac = O.charCodeAt(0);
|
||||
@ -545,11 +544,11 @@ function doit(t, args) {
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
@ -566,9 +565,9 @@ function doit(t, args) {
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : "");
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -576,16 +575,16 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o.push(O);
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o.join("");
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt, args) { return doit(tokenize(fmt), args); }
|
||||
@ -600,6 +599,6 @@ PRINTJ.sprintf = sprintf;
|
||||
PRINTJ.vsprintf = vsprintf;
|
||||
PRINTJ._doit = doit;
|
||||
PRINTJ._tokenize = tokenize;
|
||||
|
||||
PRINTJ.set_inspect = set_inspect;
|
||||
}));
|
||||
|
||||
|
2
dist/printj.min.js
vendored
2
dist/printj.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/printj.min.map
vendored
2
dist/printj.min.map
vendored
File diff suppressed because one or more lines are too long
@ -14,8 +14,8 @@ table, td {
|
||||
</head>
|
||||
<body>
|
||||
<b>PRINTJ Live Demo</b><br />
|
||||
<a href="https://git.io/printj">Source Code Repo</a><br />
|
||||
<a href="https://git.io/printj_issues">Issues? Something look weird? Click here and report an issue</a><br />
|
||||
<a href="https://github.com/SheetJS/printj">Source Code Repo</a><br />
|
||||
<a href="https://github.com/SheetJS/printj/issues">Issues? Something look weird? Click here and report an issue</a><br />
|
||||
<br />
|
||||
<pre>
|
||||
usage: var printf = PRINTJ.sprintf;
|
||||
|
581
lib/loop_code.mjs
Normal file
581
lib/loop_code.mjs
Normal file
@ -0,0 +1,581 @@
|
||||
/* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint sub:true, eqnull:true */
|
||||
/*exported PRINTJ */
|
||||
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
|
||||
/*:: declare function define(cb:()=>any):void; */
|
||||
|
||||
var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/;
|
||||
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
export const version = PRINTJ.version;
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out/*:ParsedFmt*/ = [];
|
||||
var start/*:number*/ = 0;
|
||||
|
||||
var i/*:number*/ = 0;
|
||||
var infmt/*:boolean*/ = false;
|
||||
var fmtparam/*:string*/ = "", fmtflags/*:string*/ = "", fmtwidth/*:string*/ = "", fmtprec/*:string*/ = "", fmtlen/*:string*/ = "";
|
||||
|
||||
var c/*:number*/ = 0;
|
||||
|
||||
var L/*:number*/ = fmt.length;
|
||||
|
||||
for(; i < L; ++i) {
|
||||
c = fmt.charCodeAt(i);
|
||||
if(!infmt) {
|
||||
|
||||
if(c !== 37) continue;
|
||||
|
||||
if(start < i) out.push(["L", fmt.substring(start, i)]);
|
||||
start = i;
|
||||
infmt = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c >= 48 && c < 58) {
|
||||
if(fmtprec.length) fmtprec += String.fromCharCode(c);
|
||||
else if(c == 48 && !fmtwidth.length) fmtflags += String.fromCharCode(c);
|
||||
else fmtwidth += String.fromCharCode(c);
|
||||
} else switch(c) {
|
||||
/* positional */
|
||||
case 36:
|
||||
if(fmtprec.length) fmtprec += "$";
|
||||
else if(fmtwidth.charAt(0) == "*") fmtwidth += "$";
|
||||
else { fmtparam = fmtwidth + "$"; fmtwidth = ""; }
|
||||
break;
|
||||
|
||||
/* flags */
|
||||
case 39: fmtflags += "'"; break;
|
||||
case 45: fmtflags += "-"; break;
|
||||
case 43: fmtflags += "+"; break;
|
||||
case 32: fmtflags += " "; break;
|
||||
case 35: fmtflags += "#"; break;
|
||||
|
||||
/* width and precision */
|
||||
case 46: fmtprec = "."; break;
|
||||
case 42:
|
||||
if(fmtprec.charAt(0) == ".") fmtprec += "*";
|
||||
else fmtwidth += "*";
|
||||
break;
|
||||
|
||||
/* length */
|
||||
case 104:
|
||||
case 108:
|
||||
if(fmtlen.length > 1) throw "bad length " + fmtlen + String(c);
|
||||
fmtlen += String.fromCharCode(c);
|
||||
break;
|
||||
|
||||
case 76:
|
||||
case 106:
|
||||
case 122:
|
||||
case 116:
|
||||
case 113:
|
||||
case 90:
|
||||
case 119:
|
||||
if(fmtlen !== "") throw "bad length " + fmtlen + String.fromCharCode(c);
|
||||
fmtlen = String.fromCharCode(c);
|
||||
break;
|
||||
|
||||
case 73:
|
||||
if(fmtlen !== "") throw "bad length " + fmtlen + 'I';
|
||||
fmtlen = 'I';
|
||||
break;
|
||||
|
||||
/* conversion */
|
||||
case 100:
|
||||
case 105:
|
||||
case 111:
|
||||
case 117:
|
||||
case 120:
|
||||
case 88:
|
||||
case 102:
|
||||
case 70:
|
||||
case 101:
|
||||
case 69:
|
||||
case 103:
|
||||
case 71:
|
||||
case 97:
|
||||
case 65:
|
||||
case 99:
|
||||
case 67:
|
||||
case 115:
|
||||
case 83:
|
||||
case 112:
|
||||
case 110:
|
||||
case 68:
|
||||
case 85:
|
||||
case 79:
|
||||
case 109:
|
||||
case 98:
|
||||
case 66:
|
||||
case 121:
|
||||
case 89:
|
||||
case 74:
|
||||
case 86:
|
||||
case 84:
|
||||
case 37:
|
||||
infmt = false;
|
||||
if(fmtprec.length > 1) fmtprec = fmtprec.substr(1);
|
||||
out.push([String.fromCharCode(c), fmt.substring(start, i+1), fmtparam, fmtflags, fmtwidth, fmtprec, fmtlen]);
|
||||
start = i+1;
|
||||
fmtlen = fmtprec = fmtwidth = fmtflags = fmtparam = "";
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid format string starting with |" + fmt.substring(start, i+1) + "|");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
var u_inspect/*:(o:any)=>string*/ = JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
//var o/*:Array<string>*/ = [];
|
||||
var o = "";
|
||||
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
|
||||
var Vnum/*:number*/ = 0;
|
||||
var pad/*:string*/ = "";
|
||||
for(var i/*:number*/ = 0; i < t.length; ++i) {
|
||||
var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O/*:string*/ = "";
|
||||
var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false;
|
||||
|
||||
/* flags */
|
||||
var flags/*:string*/ = m[3]/*||""*/;
|
||||
var alt/*:boolean*/ = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
|
||||
/* grab argument */
|
||||
var arg/*:any*/ = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len/*:string*/ = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
|
||||
case /*S*/ 83:
|
||||
case /*s*/ 115:
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
case /*C*/ 67:
|
||||
case /*c*/ 99:
|
||||
switch(typeof arg) {
|
||||
case "number":
|
||||
var cc/*:number*/ = arg;
|
||||
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
|
||||
else { cc &= 0xFF; O = String.fromCharCode( cc); }
|
||||
break;
|
||||
case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
|
||||
/* signed integer */
|
||||
case /*D*/ 68: bytes = 8;
|
||||
/* falls through */
|
||||
case /*d*/ 100:
|
||||
case /*i*/ 105: isnum = -1; sign = true; break;
|
||||
|
||||
/* unsigned integer */
|
||||
case /*U*/ 85: bytes = 8;
|
||||
/* falls through */
|
||||
case /*u*/ 117: isnum = -1; break;
|
||||
|
||||
/* unsigned octal */
|
||||
case /*O*/ 79: bytes = 8;
|
||||
/* falls through */
|
||||
case /*o*/ 111: isnum = -1; radix = (8); break;
|
||||
|
||||
/* unsigned hex */
|
||||
case /*x*/ 120: isnum = -1; radix = (-16); break;
|
||||
case /*X*/ 88: isnum = -1; radix = (16); break;
|
||||
|
||||
/* unsigned binary (extension) */
|
||||
case /*B*/ 66: bytes = 8;
|
||||
/* falls through */
|
||||
case /*b*/ 98: isnum = -1; radix = (2); break;
|
||||
|
||||
/* flt fegFEGaA */
|
||||
|
||||
/* floating point logic */
|
||||
case /*F*/ 70:
|
||||
case /*f*/ 102: isnum = (1); break;
|
||||
|
||||
case /*E*/ 69:
|
||||
case /*e*/ 101: isnum = (2); break;
|
||||
|
||||
case /*G*/ 71:
|
||||
case /*g*/ 103: isnum = (3); break;
|
||||
|
||||
/* floating hex */
|
||||
case /*A*/ 65:
|
||||
case /*a*/ 97: isnum = (4); break;
|
||||
|
||||
/* misc pnmJVTyY */
|
||||
|
||||
/* JS has no concept of pointers so interpret the `l` key as an address */
|
||||
case /*p*/ 112:
|
||||
Vnum = typeof arg == "number" ? arg : arg ? Number(arg.l) : -1;
|
||||
if(isNaN(Vnum)) Vnum = -1;
|
||||
if(alt) O = Vnum.toString(10);
|
||||
else {
|
||||
Vnum = Math.abs(Vnum);
|
||||
O = "0x" + Vnum.toString(16).toLowerCase();
|
||||
}
|
||||
break;
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
case /*m*/ 109:
|
||||
if(!(arg instanceof Error)) O = "Success";
|
||||
else if(arg.message) O = arg.message;
|
||||
else if(arg.errno) O = "Error number " + arg.errno;
|
||||
else O = "Error " + String(arg);
|
||||
break;
|
||||
|
||||
/* JS-specific conversions (extension) */
|
||||
case /*J*/ 74: O = (alt ? u_inspect : JSON.stringify)(arg); break;
|
||||
case /*V*/ 86: O = arg == null ? "null" : String(arg.valueOf()); break;
|
||||
case /*T*/ 84:
|
||||
if(alt) { /* from '[object %s]' extract %s */
|
||||
O = Object.prototype.toString.call(arg).substr(8);
|
||||
O = O.substr(0, O.length - 1);
|
||||
} else O = typeof arg;
|
||||
break;
|
||||
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if(width < 0) { width = -width; flags += "-"; }
|
||||
|
||||
if(isnum == -1) {
|
||||
|
||||
Vnum = Number(arg);
|
||||
|
||||
/* parse byte length field */
|
||||
|
||||
switch(len) {
|
||||
/* char */
|
||||
case "hh": { bytes = 1; } break;
|
||||
/* short */
|
||||
case "h": { bytes = 2; } break;
|
||||
|
||||
/* long */
|
||||
case "l": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* long long */
|
||||
case "L":
|
||||
case "q":
|
||||
case "ll": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* intmax_t */
|
||||
case "j": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* ptrdiff_t */
|
||||
case "t": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* size_t */
|
||||
case "z":
|
||||
case "Z": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* CRT size_t or ptrdiff_t */
|
||||
case "I":
|
||||
|
||||
{ if(bytes == 4) bytes = 8; }
|
||||
|
||||
break;
|
||||
|
||||
/* CRT wchar_t */
|
||||
case "w": break;
|
||||
}
|
||||
|
||||
/* restrict value */
|
||||
|
||||
switch(bytes) {
|
||||
case 1: Vnum = (Vnum & 0xFF); if(sign && (Vnum > 0x7F)) Vnum -= (0xFF + 1); break;
|
||||
case 2: Vnum = (Vnum & 0xFFFF); if(sign && (Vnum > 0x7FFF)) Vnum -= (0xFFFF + 1); break;
|
||||
case 4: Vnum = sign ? (Vnum | 0) : (Vnum >>> 0); break;
|
||||
default: Vnum = isNaN(Vnum) ? 0 : Math.round(Vnum); break;
|
||||
}
|
||||
|
||||
/* generate string */
|
||||
if(bytes > 4 && Vnum < 0 && !sign) {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1/*:Array<number>*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
var di/*:number*/ = d1.length - 1;
|
||||
while(Vnum > 0) {
|
||||
if((d1[di] -= (Vnum % 10)) < 0) { d1[di] += 10; d1[di-1]--; }
|
||||
--di; Vnum = Math.floor(Vnum / 10);
|
||||
}
|
||||
O = d1.join("");
|
||||
}
|
||||
} else {
|
||||
if(radix === -16) O = Vnum.toString(16).toLowerCase();
|
||||
else if(radix === 16) O = Vnum.toString(16).toUpperCase();
|
||||
else O = Vnum.toString(radix);
|
||||
}
|
||||
|
||||
/* apply precision */
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
if(!sign && alt && Vnum !== 0) switch(radix) {
|
||||
case -16: O = "0x" + O; break;
|
||||
case 16: O = "0X" + O; break;
|
||||
case 8: if(O.charAt(0) != "0") O = "0" + O; break;
|
||||
case 2: O = "0b" + O; break;
|
||||
}
|
||||
}
|
||||
|
||||
/* add sign character */
|
||||
if(sign && O.charAt(0) != "-") {
|
||||
if(flags.indexOf("+") > -1) O = "+" + O;
|
||||
else if(flags.indexOf(" ") > -1) O = " " + O;
|
||||
}
|
||||
/* width */
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
}
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if(isnum > 0) {
|
||||
|
||||
Vnum = Number(arg);
|
||||
if(arg === null) Vnum = 0/0;
|
||||
if(len == "L") bytes = 12;
|
||||
var isf/*:boolean*/ = isFinite(Vnum);
|
||||
if(!isf) { /* Infinity or NaN */
|
||||
if(Vnum < 0) O = "-";
|
||||
else if(flags.indexOf("+") > -1) O = "+";
|
||||
else if(flags.indexOf(" ") > -1) O = " ";
|
||||
O += (isNaN(Vnum)) ? "nan" : "inf";
|
||||
} else {
|
||||
var E/*:number*/ = 0;
|
||||
|
||||
if(prec == -1 && isnum != 4) prec = 6;
|
||||
|
||||
/* g/G conditional behavior */
|
||||
if(isnum == 3) {
|
||||
O = Vnum.toExponential(1);
|
||||
E = +O.substr(O.indexOf("e") + 1);
|
||||
if(prec === 0) prec = 1;
|
||||
if(prec > E && E >= -4) { isnum = (11); prec = prec -(E + 1); }
|
||||
else { isnum = (12); prec = prec - 1; }
|
||||
}
|
||||
|
||||
/* sign: workaround for negative zero */
|
||||
var sg/*:string*/ = (Vnum < 0 || 1/Vnum == -Infinity) ? "-" : "";
|
||||
if(Vnum < 0) Vnum = -Vnum;
|
||||
|
||||
switch(isnum) {
|
||||
/* f/F standard */
|
||||
case 1: case 11:
|
||||
if(Vnum < 1e21) {
|
||||
O = Vnum.toFixed(prec);
|
||||
if(isnum == 1) { if(prec===0 &&alt&& O.indexOf(".")==-1) O+="."; }
|
||||
else if(!alt) O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");
|
||||
else if(O.indexOf(".") == -1) O+= ".";
|
||||
break;
|
||||
}
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
case 2: case 12:
|
||||
O = Vnum.toExponential(prec);
|
||||
E = O.indexOf("e");
|
||||
if(O.length - E === 3) O = O.substr(0, E+2) + "0" + O.substr(E+2);
|
||||
if(alt && O.indexOf(".") == -1) O = O.substr(0,E) +"."+ O.substr(E);
|
||||
else if(!alt && isnum == 12) O = O.replace(/\.0*e/, "e").replace(/\.(\d*[1-9])0*e/, ".$1e");
|
||||
break;
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac/*:number*/ = O.charCodeAt(0);
|
||||
if(ac == 48) {
|
||||
ac = 2; E = -4; Vnum *= 16;
|
||||
while(O.charCodeAt(ac++) == 48) { E -= 4; Vnum *= 16; }
|
||||
O = Vnum.toString(16);
|
||||
ac = O.charCodeAt(0);
|
||||
}
|
||||
|
||||
var ai/*:number*/ = O.indexOf(".");
|
||||
if(O.indexOf("(") > -1) {
|
||||
/* IE exponential form */
|
||||
var am/*:?Array<any>*/ = O.match(/\(e(.*)\)/);
|
||||
var ae/*:number*/ = am ? (+am[1]) : 0;
|
||||
E += 4 * ae; Vnum /= Math.pow(16, ae);
|
||||
} else if(ai > 1) {
|
||||
E += 4 * (ai - 1); Vnum /= Math.pow(16, ai - 1);
|
||||
} else if(ai == -1) {
|
||||
E += 4 * (O.length - 1); Vnum /= Math.pow(16, O.length - 1);
|
||||
}
|
||||
|
||||
/* at this point 1 <= Vnum < 16 */
|
||||
|
||||
if(bytes > 8) {
|
||||
if(ac < 50) { E -= 3; Vnum *= 8; }
|
||||
else if(ac < 52) { E -= 2; Vnum *= 4; }
|
||||
else if(ac < 56) { E -= 1; Vnum *= 2; }
|
||||
/* at this point 8 <= Vnum < 16 */
|
||||
} else {
|
||||
if(ac >= 56) { E += 3; Vnum /= 8; }
|
||||
else if(ac >= 52) { E += 2; Vnum /= 4; }
|
||||
else if(ac >= 50) { E += 1; Vnum /= 2; }
|
||||
/* at this point 1 <= Vnum < 2 */
|
||||
}
|
||||
|
||||
O = Vnum.toString(16);
|
||||
if(O.length > 1) {
|
||||
if(O.length > prec+2 && O.charCodeAt(prec+2) >= 56) {
|
||||
var _f/*:boolean*/ = O.charCodeAt(0) == 102;
|
||||
O = (Vnum + 8 * Math.pow(16, -prec-1)).toString(16);
|
||||
if(_f && O.charCodeAt(0) == 49) E += 4;
|
||||
}
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
}
|
||||
|
||||
if(sg === "") {
|
||||
if(flags.indexOf("+") > -1) sg = "+";
|
||||
else if(flags.indexOf(" ") > -1) sg = " ";
|
||||
}
|
||||
|
||||
O = sg + O;
|
||||
}
|
||||
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
}
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); }
|
||||
|
||||
function sprintf(/*:: ...argz*/)/*:string*/ {
|
||||
var args/*:Array<any>*/ = new Array(arguments.length - 1);
|
||||
for(var i/*:number*/ = 0; i < args.length; ++i) args[i] = arguments[i+1];
|
||||
return doit(tokenize(arguments[0]), args);
|
||||
}
|
||||
|
||||
export { sprintf, vsprintf, set_inspect };
|
||||
|
3
misc/import_map.json
Normal file
3
misc/import_map.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"imports": {}
|
||||
}
|
11
misc/test.deno.jsonc
Normal file
11
misc/test.deno.jsonc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"deno.ns",
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"dom.asynciterable"
|
||||
]
|
||||
},
|
||||
"importMap": "./import_map.json"
|
||||
}
|
35
package.json
35
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "printj",
|
||||
"version": "1.1.2",
|
||||
"version": "1.3.2",
|
||||
"author": "sheetjs",
|
||||
"description": "Pure-JS printf",
|
||||
"keywords": [
|
||||
@ -10,9 +10,10 @@
|
||||
"string"
|
||||
],
|
||||
"bin": {
|
||||
"printj": "./bin/printj.njs"
|
||||
"printj": "bin/printj.njs"
|
||||
},
|
||||
"main": "./printj",
|
||||
"main": "printj.js",
|
||||
"module": "printj.mjs",
|
||||
"types": "types",
|
||||
"browser": {
|
||||
"process": false,
|
||||
@ -21,16 +22,22 @@
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "~2.5.3",
|
||||
"blanket": "~1.2.3",
|
||||
"@sheetjs/uglify-js": "~2.7.3",
|
||||
"@types/node": "^8.0.7",
|
||||
"dtslint": "^0.1.2",
|
||||
"typescript": "2.2.0"
|
||||
"alex": "10.0.0",
|
||||
"blanket": "~1.2.3",
|
||||
"dtslint": "4.2.1",
|
||||
"eslint": "6.8.0",
|
||||
"eslint-plugin-html": "6.0.0",
|
||||
"eslint-plugin-json": "1.4.0",
|
||||
"jscs": "3.0.7",
|
||||
"jshint": "2.9.6",
|
||||
"markdown-spellcheck": "1.3.1",
|
||||
"mocha": "~2.5.3"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/SheetJS/printj.git"
|
||||
"url": "https://git.sheetjs.com/sheetjs/printj.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "make test",
|
||||
@ -45,13 +52,17 @@
|
||||
},
|
||||
"alex": {
|
||||
"allow": [
|
||||
"period"
|
||||
"whitespace",
|
||||
"special",
|
||||
"period",
|
||||
"just",
|
||||
"invalid"
|
||||
]
|
||||
},
|
||||
"homepage": "http://sheetjs.com/opensource",
|
||||
"files": ["printj.js", "bin/printj.njs", "LICENSE", "README.md", "dist/*.js", "dist/*.map", "dist/LICENSE", "types/index.d.ts", "types/*.json"],
|
||||
"homepage": "https://sheetjs.com/",
|
||||
"files": ["printj.js", "printj.mjs", "bin/printj.njs", "LICENSE", "README.md", "shim.js", "dist/*.js", "dist/*.map", "dist/LICENSE", "types/index.d.ts", "types/*.json"],
|
||||
"bugs": {
|
||||
"url": "https://github.com/SheetJS/printj/issues"
|
||||
"url": "https://git.sheetjs.com/sheetjs/printj/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
|
@ -4,6 +4,7 @@
|
||||
/*exported PRINTJ */
|
||||
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
|
||||
/*:: declare function define(cb:()=>any):void; */
|
||||
|
||||
var PRINTJ/*:PRINTJModule*/;
|
||||
(function (factory/*:(a:any)=>void*/)/*:void*/ {
|
||||
/*jshint ignore:start */
|
||||
@ -27,9 +28,12 @@ var PRINTJ/*:PRINTJModule*/;
|
||||
/*jshint ignore:end */
|
||||
}(function(PRINTJ/*:PRINTJModule*/) {
|
||||
|
||||
PRINTJ.version = '1.1.2';
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out/*:ParsedFmt*/ = [];
|
||||
var start/*:number*/ = 0;
|
||||
|
||||
@ -148,24 +152,19 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return out;
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
|
||||
var padstr/*:{[s:string]:string}*/ = {
|
||||
" ": " ",
|
||||
"0": "000000000000000000000000000000000",
|
||||
"7": "777777777777777777777777777777777",
|
||||
"f": "fffffffffffffffffffffffffffffffff"
|
||||
};
|
||||
|
||||
/*:: var util = require('util'); */
|
||||
/*global process:true, util:true, require:true */
|
||||
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
|
||||
var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var o/*:Array<string>*/ = [];
|
||||
//var o/*:Array<string>*/ = [];
|
||||
var o = "";
|
||||
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
|
||||
var Vnum/*:number*/ = 0;
|
||||
var pad/*:string*/ = "";
|
||||
@ -173,26 +172,26 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o.push(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o.push("%"); continue; }
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O/*:string*/ = "";
|
||||
var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false;
|
||||
|
||||
/* flags */
|
||||
var flags/*:string*/ = m[3]||"";
|
||||
var flags/*:string*/ = m[3]/*||""*/;
|
||||
var alt/*:boolean*/ = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2])-1;
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; }
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
@ -201,7 +200,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
var arg/*:any*/ = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len/*:string*/ = m[6] || "";
|
||||
var len/*:string*/ = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
@ -211,7 +210,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
@ -226,7 +225,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
@ -287,7 +286,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
@ -311,10 +311,10 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
@ -376,16 +376,16 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O;
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O;
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O;
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1/*:Array<number>*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
@ -406,8 +406,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1);
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
@ -428,10 +428,10 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : "");
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -439,7 +439,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -486,8 +486,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : "");
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
@ -501,7 +501,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;}
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac/*:number*/ = O.charCodeAt(0);
|
||||
@ -548,11 +548,11 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
@ -569,9 +569,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : "");
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -579,16 +579,16 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o.push(O);
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o.join("");
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); }
|
||||
@ -603,6 +603,6 @@ PRINTJ.sprintf = sprintf;
|
||||
PRINTJ.vsprintf = vsprintf;
|
||||
PRINTJ._doit = doit;
|
||||
PRINTJ._tokenize = tokenize;
|
||||
|
||||
PRINTJ.set_inspect = set_inspect;
|
||||
}));
|
||||
|
||||
|
91
printj.js
91
printj.js
@ -25,9 +25,12 @@ var PRINTJ;
|
||||
/*jshint ignore:end */
|
||||
}(function(PRINTJ) {
|
||||
|
||||
PRINTJ.version = '1.1.2';
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt) {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out = [];
|
||||
var start = 0;
|
||||
|
||||
@ -146,23 +149,18 @@ function tokenize(fmt) {
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return out;
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
|
||||
var padstr = {
|
||||
" ": " ",
|
||||
"0": "000000000000000000000000000000000",
|
||||
"7": "777777777777777777777777777777777",
|
||||
"f": "fffffffffffffffffffffffffffffffff"
|
||||
};
|
||||
|
||||
/*global process:true, util:true, require:true */
|
||||
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
|
||||
var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t, args) {
|
||||
var o = [];
|
||||
//var o = [];
|
||||
var o = "";
|
||||
var argidx = 0, idx = 0;
|
||||
var Vnum = 0;
|
||||
var pad = "";
|
||||
@ -170,26 +168,26 @@ function doit(t, args) {
|
||||
var m = t[i], c = (m[0]).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o.push(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o.push("%"); continue; }
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O = "";
|
||||
var isnum = 0, radix = 10, bytes = 4, sign = false;
|
||||
|
||||
/* flags */
|
||||
var flags = m[3]||"";
|
||||
var flags = m[3]/*||""*/;
|
||||
var alt = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2])-1;
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; }
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
@ -198,7 +196,7 @@ function doit(t, args) {
|
||||
var arg = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len = m[6] || "";
|
||||
var len = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
@ -208,7 +206,7 @@ function doit(t, args) {
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
@ -223,7 +221,7 @@ function doit(t, args) {
|
||||
case "string": O = arg.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
@ -284,7 +282,8 @@ function doit(t, args) {
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
@ -308,10 +307,10 @@ function doit(t, args) {
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
@ -373,16 +372,16 @@ function doit(t, args) {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O;
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O;
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O;
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
@ -403,8 +402,8 @@ function doit(t, args) {
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1);
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
@ -425,10 +424,10 @@ function doit(t, args) {
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : "");
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -436,7 +435,7 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -483,8 +482,8 @@ function doit(t, args) {
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : "");
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
@ -498,7 +497,7 @@ function doit(t, args) {
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;}
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac = O.charCodeAt(0);
|
||||
@ -545,11 +544,11 @@ function doit(t, args) {
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
@ -566,9 +565,9 @@ function doit(t, args) {
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "");
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : "");
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
@ -576,16 +575,16 @@ function doit(t, args) {
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O;
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o.push(O);
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o.join("");
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt, args) { return doit(tokenize(fmt), args); }
|
||||
@ -600,6 +599,6 @@ PRINTJ.sprintf = sprintf;
|
||||
PRINTJ.vsprintf = vsprintf;
|
||||
PRINTJ._doit = doit;
|
||||
PRINTJ._tokenize = tokenize;
|
||||
|
||||
PRINTJ.set_inspect = set_inspect;
|
||||
}));
|
||||
|
||||
|
581
printj.mjs
Normal file
581
printj.mjs
Normal file
@ -0,0 +1,581 @@
|
||||
/* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint sub:true, eqnull:true */
|
||||
/*exported PRINTJ */
|
||||
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
|
||||
/*:: declare function define(cb:()=>any):void; */
|
||||
|
||||
var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/;
|
||||
|
||||
PRINTJ.version = '1.3.2';
|
||||
|
||||
export const version = PRINTJ.version;
|
||||
|
||||
var tcache = {};
|
||||
|
||||
function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
|
||||
if(tcache[fmt]) return tcache[fmt];
|
||||
var out/*:ParsedFmt*/ = [];
|
||||
var start/*:number*/ = 0;
|
||||
|
||||
var i/*:number*/ = 0;
|
||||
var infmt/*:boolean*/ = false;
|
||||
var fmtparam/*:string*/ = "", fmtflags/*:string*/ = "", fmtwidth/*:string*/ = "", fmtprec/*:string*/ = "", fmtlen/*:string*/ = "";
|
||||
|
||||
var c/*:number*/ = 0;
|
||||
|
||||
var L/*:number*/ = fmt.length;
|
||||
|
||||
for(; i < L; ++i) {
|
||||
c = fmt.charCodeAt(i);
|
||||
if(!infmt) {
|
||||
|
||||
if(c !== 37) continue;
|
||||
|
||||
if(start < i) out.push(["L", fmt.substring(start, i)]);
|
||||
start = i;
|
||||
infmt = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c >= 48 && c < 58) {
|
||||
if(fmtprec.length) fmtprec += String.fromCharCode(c);
|
||||
else if(c == 48 && !fmtwidth.length) fmtflags += String.fromCharCode(c);
|
||||
else fmtwidth += String.fromCharCode(c);
|
||||
} else switch(c) {
|
||||
/* positional */
|
||||
case 36:
|
||||
if(fmtprec.length) fmtprec += "$";
|
||||
else if(fmtwidth.charAt(0) == "*") fmtwidth += "$";
|
||||
else { fmtparam = fmtwidth + "$"; fmtwidth = ""; }
|
||||
break;
|
||||
|
||||
/* flags */
|
||||
case 39: fmtflags += "'"; break;
|
||||
case 45: fmtflags += "-"; break;
|
||||
case 43: fmtflags += "+"; break;
|
||||
case 32: fmtflags += " "; break;
|
||||
case 35: fmtflags += "#"; break;
|
||||
|
||||
/* width and precision */
|
||||
case 46: fmtprec = "."; break;
|
||||
case 42:
|
||||
if(fmtprec.charAt(0) == ".") fmtprec += "*";
|
||||
else fmtwidth += "*";
|
||||
break;
|
||||
|
||||
/* length */
|
||||
case 104:
|
||||
case 108:
|
||||
if(fmtlen.length > 1) throw "bad length " + fmtlen + String(c);
|
||||
fmtlen += String.fromCharCode(c);
|
||||
break;
|
||||
|
||||
case 76:
|
||||
case 106:
|
||||
case 122:
|
||||
case 116:
|
||||
case 113:
|
||||
case 90:
|
||||
case 119:
|
||||
if(fmtlen !== "") throw "bad length " + fmtlen + String.fromCharCode(c);
|
||||
fmtlen = String.fromCharCode(c);
|
||||
break;
|
||||
|
||||
case 73:
|
||||
if(fmtlen !== "") throw "bad length " + fmtlen + 'I';
|
||||
fmtlen = 'I';
|
||||
break;
|
||||
|
||||
/* conversion */
|
||||
case 100:
|
||||
case 105:
|
||||
case 111:
|
||||
case 117:
|
||||
case 120:
|
||||
case 88:
|
||||
case 102:
|
||||
case 70:
|
||||
case 101:
|
||||
case 69:
|
||||
case 103:
|
||||
case 71:
|
||||
case 97:
|
||||
case 65:
|
||||
case 99:
|
||||
case 67:
|
||||
case 115:
|
||||
case 83:
|
||||
case 112:
|
||||
case 110:
|
||||
case 68:
|
||||
case 85:
|
||||
case 79:
|
||||
case 109:
|
||||
case 98:
|
||||
case 66:
|
||||
case 121:
|
||||
case 89:
|
||||
case 74:
|
||||
case 86:
|
||||
case 84:
|
||||
case 37:
|
||||
infmt = false;
|
||||
if(fmtprec.length > 1) fmtprec = fmtprec.substr(1);
|
||||
out.push([String.fromCharCode(c), fmt.substring(start, i+1), fmtparam, fmtflags, fmtwidth, fmtprec, fmtlen]);
|
||||
start = i+1;
|
||||
fmtlen = fmtprec = fmtwidth = fmtflags = fmtparam = "";
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid format string starting with |" + fmt.substring(start, i+1) + "|");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(start < fmt.length) out.push(["L", fmt.substring(start)]);
|
||||
return (tcache[fmt] = out);
|
||||
}
|
||||
|
||||
var u_inspect/*:(o:any)=>string*/ = JSON.stringify;
|
||||
|
||||
function set_inspect(inspect) { u_inspect = inspect; }
|
||||
|
||||
function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
|
||||
//var o/*:Array<string>*/ = [];
|
||||
var o = "";
|
||||
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
|
||||
var Vnum/*:number*/ = 0;
|
||||
var pad/*:string*/ = "";
|
||||
for(var i/*:number*/ = 0; i < t.length; ++i) {
|
||||
var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
|
||||
/* m order: conv full param flags width prec length */
|
||||
|
||||
if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; }
|
||||
if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; }
|
||||
|
||||
var O/*:string*/ = "";
|
||||
var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false;
|
||||
|
||||
/* flags */
|
||||
var flags/*:string*/ = m[3]/*||""*/;
|
||||
var alt/*:boolean*/ = flags.indexOf("#") > -1;
|
||||
|
||||
/* position */
|
||||
if(m[2]) argidx = parseInt(m[2], 10)-1;
|
||||
/* %m special case */
|
||||
else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; }
|
||||
|
||||
/* grab width */
|
||||
var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; }
|
||||
|
||||
/* grab precision */
|
||||
var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; }
|
||||
|
||||
/* position not specified */
|
||||
if(!m[2]) argidx = idx++;
|
||||
|
||||
/* grab argument */
|
||||
var arg/*:any*/ = args[argidx];
|
||||
|
||||
/* grab length */
|
||||
var len/*:string*/ = m[6]/* || ""*/;
|
||||
|
||||
switch(c) {
|
||||
/* str cCsS */
|
||||
|
||||
case /*S*/ 83:
|
||||
case /*s*/ 115:
|
||||
/* only valid flag is "-" for left justification */
|
||||
O = String(arg);
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* first char of string or convert */
|
||||
case /*C*/ 67:
|
||||
case /*c*/ 99:
|
||||
switch(typeof arg) {
|
||||
case "number":
|
||||
var cc/*:number*/ = arg;
|
||||
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
|
||||
else { cc &= 0xFF; O = String.fromCharCode( cc); }
|
||||
break;
|
||||
case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break;
|
||||
default: O = String(arg).charAt(0);
|
||||
}
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
/* int diDuUoOxXbB */
|
||||
|
||||
/* signed integer */
|
||||
case /*D*/ 68: bytes = 8;
|
||||
/* falls through */
|
||||
case /*d*/ 100:
|
||||
case /*i*/ 105: isnum = -1; sign = true; break;
|
||||
|
||||
/* unsigned integer */
|
||||
case /*U*/ 85: bytes = 8;
|
||||
/* falls through */
|
||||
case /*u*/ 117: isnum = -1; break;
|
||||
|
||||
/* unsigned octal */
|
||||
case /*O*/ 79: bytes = 8;
|
||||
/* falls through */
|
||||
case /*o*/ 111: isnum = -1; radix = (8); break;
|
||||
|
||||
/* unsigned hex */
|
||||
case /*x*/ 120: isnum = -1; radix = (-16); break;
|
||||
case /*X*/ 88: isnum = -1; radix = (16); break;
|
||||
|
||||
/* unsigned binary (extension) */
|
||||
case /*B*/ 66: bytes = 8;
|
||||
/* falls through */
|
||||
case /*b*/ 98: isnum = -1; radix = (2); break;
|
||||
|
||||
/* flt fegFEGaA */
|
||||
|
||||
/* floating point logic */
|
||||
case /*F*/ 70:
|
||||
case /*f*/ 102: isnum = (1); break;
|
||||
|
||||
case /*E*/ 69:
|
||||
case /*e*/ 101: isnum = (2); break;
|
||||
|
||||
case /*G*/ 71:
|
||||
case /*g*/ 103: isnum = (3); break;
|
||||
|
||||
/* floating hex */
|
||||
case /*A*/ 65:
|
||||
case /*a*/ 97: isnum = (4); break;
|
||||
|
||||
/* misc pnmJVTyY */
|
||||
|
||||
/* JS has no concept of pointers so interpret the `l` key as an address */
|
||||
case /*p*/ 112:
|
||||
Vnum = typeof arg == "number" ? arg : arg ? Number(arg.l) : -1;
|
||||
if(isNaN(Vnum)) Vnum = -1;
|
||||
if(alt) O = Vnum.toString(10);
|
||||
else {
|
||||
Vnum = Math.abs(Vnum);
|
||||
O = "0x" + Vnum.toString(16).toLowerCase();
|
||||
}
|
||||
break;
|
||||
|
||||
/* store length in the `len` key */
|
||||
case /*n*/ 110:
|
||||
if(arg) { arg.len = o.length; }
|
||||
//if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
|
||||
continue;
|
||||
|
||||
/* process error */
|
||||
case /*m*/ 109:
|
||||
if(!(arg instanceof Error)) O = "Success";
|
||||
else if(arg.message) O = arg.message;
|
||||
else if(arg.errno) O = "Error number " + arg.errno;
|
||||
else O = "Error " + String(arg);
|
||||
break;
|
||||
|
||||
/* JS-specific conversions (extension) */
|
||||
case /*J*/ 74: O = (alt ? u_inspect : JSON.stringify)(arg); break;
|
||||
case /*V*/ 86: O = arg == null ? "null" : String(arg.valueOf()); break;
|
||||
case /*T*/ 84:
|
||||
if(alt) { /* from '[object %s]' extract %s */
|
||||
O = Object.prototype.toString.call(arg).substr(8);
|
||||
O = O.substr(0, O.length - 1);
|
||||
} else O = typeof arg;
|
||||
break;
|
||||
|
||||
/* boolean (extension) */
|
||||
case /*Y*/ 89:
|
||||
case /*y*/ 121:
|
||||
O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false");
|
||||
if(c == /*Y*/ 89) O = O.toUpperCase();
|
||||
if( prec >= 0) O = O.substr(0, prec);
|
||||
if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } }
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if(width < 0) { width = -width; flags += "-"; }
|
||||
|
||||
if(isnum == -1) {
|
||||
|
||||
Vnum = Number(arg);
|
||||
|
||||
/* parse byte length field */
|
||||
|
||||
switch(len) {
|
||||
/* char */
|
||||
case "hh": { bytes = 1; } break;
|
||||
/* short */
|
||||
case "h": { bytes = 2; } break;
|
||||
|
||||
/* long */
|
||||
case "l": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* long long */
|
||||
case "L":
|
||||
case "q":
|
||||
case "ll": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* intmax_t */
|
||||
case "j": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* ptrdiff_t */
|
||||
case "t": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* size_t */
|
||||
case "z":
|
||||
case "Z": { if(bytes == 4) bytes = 8; } break;
|
||||
|
||||
/* CRT size_t or ptrdiff_t */
|
||||
case "I":
|
||||
|
||||
{ if(bytes == 4) bytes = 8; }
|
||||
|
||||
break;
|
||||
|
||||
/* CRT wchar_t */
|
||||
case "w": break;
|
||||
}
|
||||
|
||||
/* restrict value */
|
||||
|
||||
switch(bytes) {
|
||||
case 1: Vnum = (Vnum & 0xFF); if(sign && (Vnum > 0x7F)) Vnum -= (0xFF + 1); break;
|
||||
case 2: Vnum = (Vnum & 0xFFFF); if(sign && (Vnum > 0x7FFF)) Vnum -= (0xFFFF + 1); break;
|
||||
case 4: Vnum = sign ? (Vnum | 0) : (Vnum >>> 0); break;
|
||||
default: Vnum = isNaN(Vnum) ? 0 : Math.round(Vnum); break;
|
||||
}
|
||||
|
||||
/* generate string */
|
||||
if(bytes > 4 && Vnum < 0 && !sign) {
|
||||
if(radix == 16 || radix == -16) {
|
||||
O = (Vnum>>>0).toString(16);
|
||||
Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32));
|
||||
O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O;
|
||||
O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O;
|
||||
if(radix == 16) O = O.toUpperCase();
|
||||
} else if(radix == 8) {
|
||||
O = (Vnum>>>0).toString(8);
|
||||
O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O;
|
||||
Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30));
|
||||
O = (Vnum>>>0).toString(8) + O.substr(O.length - 10);
|
||||
O = O.substr(O.length - 20);
|
||||
O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O;
|
||||
} else {
|
||||
Vnum = (-Vnum) % 1e16;
|
||||
var d1/*:Array<number>*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];
|
||||
var di/*:number*/ = d1.length - 1;
|
||||
while(Vnum > 0) {
|
||||
if((d1[di] -= (Vnum % 10)) < 0) { d1[di] += 10; d1[di-1]--; }
|
||||
--di; Vnum = Math.floor(Vnum / 10);
|
||||
}
|
||||
O = d1.join("");
|
||||
}
|
||||
} else {
|
||||
if(radix === -16) O = Vnum.toString(16).toLowerCase();
|
||||
else if(radix === 16) O = Vnum.toString(16).toUpperCase();
|
||||
else O = Vnum.toString(radix);
|
||||
}
|
||||
|
||||
/* apply precision */
|
||||
if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */
|
||||
else {
|
||||
if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) {
|
||||
if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O;
|
||||
else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1);
|
||||
}
|
||||
|
||||
/* add prefix for # form */
|
||||
if(!sign && alt && Vnum !== 0) switch(radix) {
|
||||
case -16: O = "0x" + O; break;
|
||||
case 16: O = "0X" + O; break;
|
||||
case 8: if(O.charAt(0) != "0") O = "0" + O; break;
|
||||
case 2: O = "0b" + O; break;
|
||||
}
|
||||
}
|
||||
|
||||
/* add sign character */
|
||||
if(sign && O.charAt(0) != "-") {
|
||||
if(flags.indexOf("+") > -1) O = "+" + O;
|
||||
else if(flags.indexOf(" ") > -1) O = " " + O;
|
||||
}
|
||||
/* width */
|
||||
if(width > 0) {
|
||||
if(O.length < width) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) {
|
||||
if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O;
|
||||
pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
}
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if(isnum > 0) {
|
||||
|
||||
Vnum = Number(arg);
|
||||
if(arg === null) Vnum = 0/0;
|
||||
if(len == "L") bytes = 12;
|
||||
var isf/*:boolean*/ = isFinite(Vnum);
|
||||
if(!isf) { /* Infinity or NaN */
|
||||
if(Vnum < 0) O = "-";
|
||||
else if(flags.indexOf("+") > -1) O = "+";
|
||||
else if(flags.indexOf(" ") > -1) O = " ";
|
||||
O += (isNaN(Vnum)) ? "nan" : "inf";
|
||||
} else {
|
||||
var E/*:number*/ = 0;
|
||||
|
||||
if(prec == -1 && isnum != 4) prec = 6;
|
||||
|
||||
/* g/G conditional behavior */
|
||||
if(isnum == 3) {
|
||||
O = Vnum.toExponential(1);
|
||||
E = +O.substr(O.indexOf("e") + 1);
|
||||
if(prec === 0) prec = 1;
|
||||
if(prec > E && E >= -4) { isnum = (11); prec = prec -(E + 1); }
|
||||
else { isnum = (12); prec = prec - 1; }
|
||||
}
|
||||
|
||||
/* sign: workaround for negative zero */
|
||||
var sg/*:string*/ = (Vnum < 0 || 1/Vnum == -Infinity) ? "-" : "";
|
||||
if(Vnum < 0) Vnum = -Vnum;
|
||||
|
||||
switch(isnum) {
|
||||
/* f/F standard */
|
||||
case 1: case 11:
|
||||
if(Vnum < 1e21) {
|
||||
O = Vnum.toFixed(prec);
|
||||
if(isnum == 1) { if(prec===0 &&alt&& O.indexOf(".")==-1) O+="."; }
|
||||
else if(!alt) O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");
|
||||
else if(O.indexOf(".") == -1) O+= ".";
|
||||
break;
|
||||
}
|
||||
O = Vnum.toExponential(20);
|
||||
E = +O.substr(O.indexOf("e")+1);
|
||||
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
|
||||
O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : "");
|
||||
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
break;
|
||||
|
||||
/* e/E exponential */
|
||||
case 2: case 12:
|
||||
O = Vnum.toExponential(prec);
|
||||
E = O.indexOf("e");
|
||||
if(O.length - E === 3) O = O.substr(0, E+2) + "0" + O.substr(E+2);
|
||||
if(alt && O.indexOf(".") == -1) O = O.substr(0,E) +"."+ O.substr(E);
|
||||
else if(!alt && isnum == 12) O = O.replace(/\.0*e/, "e").replace(/\.(\d*[1-9])0*e/, ".$1e");
|
||||
break;
|
||||
|
||||
/* a/A hex */
|
||||
case 4:
|
||||
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;}
|
||||
O = Vnum.toString(16);
|
||||
/* First char 0-9 */
|
||||
var ac/*:number*/ = O.charCodeAt(0);
|
||||
if(ac == 48) {
|
||||
ac = 2; E = -4; Vnum *= 16;
|
||||
while(O.charCodeAt(ac++) == 48) { E -= 4; Vnum *= 16; }
|
||||
O = Vnum.toString(16);
|
||||
ac = O.charCodeAt(0);
|
||||
}
|
||||
|
||||
var ai/*:number*/ = O.indexOf(".");
|
||||
if(O.indexOf("(") > -1) {
|
||||
/* IE exponential form */
|
||||
var am/*:?Array<any>*/ = O.match(/\(e(.*)\)/);
|
||||
var ae/*:number*/ = am ? (+am[1]) : 0;
|
||||
E += 4 * ae; Vnum /= Math.pow(16, ae);
|
||||
} else if(ai > 1) {
|
||||
E += 4 * (ai - 1); Vnum /= Math.pow(16, ai - 1);
|
||||
} else if(ai == -1) {
|
||||
E += 4 * (O.length - 1); Vnum /= Math.pow(16, O.length - 1);
|
||||
}
|
||||
|
||||
/* at this point 1 <= Vnum < 16 */
|
||||
|
||||
if(bytes > 8) {
|
||||
if(ac < 50) { E -= 3; Vnum *= 8; }
|
||||
else if(ac < 52) { E -= 2; Vnum *= 4; }
|
||||
else if(ac < 56) { E -= 1; Vnum *= 2; }
|
||||
/* at this point 8 <= Vnum < 16 */
|
||||
} else {
|
||||
if(ac >= 56) { E += 3; Vnum /= 8; }
|
||||
else if(ac >= 52) { E += 2; Vnum /= 4; }
|
||||
else if(ac >= 50) { E += 1; Vnum /= 2; }
|
||||
/* at this point 1 <= Vnum < 2 */
|
||||
}
|
||||
|
||||
O = Vnum.toString(16);
|
||||
if(O.length > 1) {
|
||||
if(O.length > prec+2 && O.charCodeAt(prec+2) >= 56) {
|
||||
var _f/*:boolean*/ = O.charCodeAt(0) == 102;
|
||||
O = (Vnum + 8 * Math.pow(16, -prec-1)).toString(16);
|
||||
if(_f && O.charCodeAt(0) == 49) E += 4;
|
||||
}
|
||||
if(prec > 0) {
|
||||
O = O.substr(0, prec + 2);
|
||||
if(O.length < prec + 2) {
|
||||
if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1);
|
||||
else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "");
|
||||
}
|
||||
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
|
||||
} else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : "");
|
||||
else if(alt) O = O + ".";
|
||||
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
|
||||
break;
|
||||
}
|
||||
|
||||
if(sg === "") {
|
||||
if(flags.indexOf("+") > -1) sg = "+";
|
||||
else if(flags.indexOf(" ") > -1) sg = " ";
|
||||
}
|
||||
|
||||
O = sg + O;
|
||||
}
|
||||
|
||||
/* width */
|
||||
if(width > O.length) {
|
||||
if(flags.indexOf("-") > -1) {
|
||||
O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "");
|
||||
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
|
||||
pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : "");
|
||||
if(O.charCodeAt(0) < 48) {
|
||||
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
|
||||
else O = O.substr(0,1) + pad + O.substring(1);
|
||||
}
|
||||
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
|
||||
else O = pad + O;
|
||||
} else {
|
||||
O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O;
|
||||
}
|
||||
}
|
||||
if(c < 96) O = O.toUpperCase();
|
||||
|
||||
}
|
||||
|
||||
o += /*.push*/(O);
|
||||
}
|
||||
return o/*.join("")*/;
|
||||
}
|
||||
|
||||
function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); }
|
||||
|
||||
function sprintf(/*:: ...argz*/)/*:string*/ {
|
||||
var args/*:Array<any>*/ = new Array(arguments.length - 1);
|
||||
for(var i/*:number*/ = 0; i < args.length; ++i) args[i] = arguments[i+1];
|
||||
return doit(tokenize(arguments[0]), args);
|
||||
}
|
||||
|
||||
export { sprintf, vsprintf, set_inspect };
|
||||
|
39
shim.js
39
shim.js
@ -235,3 +235,42 @@ if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) {
|
||||
return target;
|
||||
};
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
|
||||
if (!String.prototype.repeat) {
|
||||
String.prototype.repeat = function(count) {
|
||||
'use strict';
|
||||
if (this == null) {
|
||||
throw new TypeError('can\'t convert ' + this + ' to object');
|
||||
}
|
||||
var str = '' + this;
|
||||
count = +count;
|
||||
if (count != count) {
|
||||
count = 0;
|
||||
}
|
||||
if (count < 0) {
|
||||
throw new RangeError('repeat count must be non-negative');
|
||||
}
|
||||
if (count == Infinity) {
|
||||
throw new RangeError('repeat count must be less than infinity');
|
||||
}
|
||||
count = Math.floor(count);
|
||||
if (str.length == 0 || count == 0) {
|
||||
return '';
|
||||
}
|
||||
// Ensuring count is a 31-bit integer allows us to heavily optimize the
|
||||
// main part. But anyway, most current (August 2014) browsers can't handle
|
||||
// strings 1 << 28 chars or longer, so:
|
||||
if (str.length * count >= 1 << 28) {
|
||||
throw new RangeError('repeat count must not overflow maximum string size');
|
||||
}
|
||||
var maxCount = str.length * count;
|
||||
count = Math.floor(Math.log(count) / Math.log(2));
|
||||
while (count) {
|
||||
str += str;
|
||||
count--;
|
||||
}
|
||||
str += str.substring(0, maxCount - str.length);
|
||||
return str;
|
||||
};
|
||||
}
|
||||
|
13
test.js
13
test.js
@ -1,8 +1,11 @@
|
||||
/* vim: set ts=2: */
|
||||
/* eslint-env mocha */
|
||||
/*global assert:true */
|
||||
var X;
|
||||
var IMPLS = {}, IMPLA = [], IMPL = [];
|
||||
if(typeof require !== 'undefined') {
|
||||
assert = require('assert');
|
||||
require('./shim');
|
||||
X=require('./');
|
||||
IMPL = require("./lib/impl.json");
|
||||
IMPL.forEach(function(impl, i) { IMPLS[impl] = IMPLA[i] = require("./lib/" + impl); });
|
||||
@ -171,6 +174,13 @@ describe('special cases', function() {
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", undefined), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", null), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
});
|
||||
if(typeof JSON !== "undefined") it('correctly handles JSON conversion: J', function() {
|
||||
var data = ({a:1});
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
X.set_inspect(function(data) { return "|" + JSON.stringify(data) + "|"; });
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
assert.equal(sprintf("%#J", data), "|" + JSON.stringify(data) + "|");
|
||||
});
|
||||
it('handles dynamic specifiers', function() {
|
||||
assert.equal(sprintf("|%5s|", "sheetjs"), "|sheetjs|");
|
||||
assert.equal(sprintf("|%*s|", 5, "sheetjs"), "|sheetjs|");
|
||||
@ -190,4 +200,7 @@ describe('special cases', function() {
|
||||
assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |");
|
||||
assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|");
|
||||
});
|
||||
it('handles long strings', function() {
|
||||
for(var i = 10; i <= 1000; i+= 10) assert.equal(sprintf("%" + i + "s", "abc").length, i);
|
||||
});
|
||||
});
|
||||
|
147
test.ts
Normal file
147
test.ts
Normal file
@ -0,0 +1,147 @@
|
||||
/* vim: set ts=2: */
|
||||
/* eslint-env mocha */
|
||||
import * as assert_ from 'https://deno.land/std/testing/asserts.ts';
|
||||
const assert: any = {...assert_};
|
||||
assert.throws = function(f: () => void) { assert.assertThrows(function() { try { f(); } catch(e) { throw e instanceof Error ? e : new Error(e); }})};
|
||||
assert.doesNotThrow = function(f: ()=>void) { f(); };
|
||||
assert.equal = assert.assertEquals;
|
||||
assert.notEqual = assert.assertNotEquals;
|
||||
assert.deepEqual = (x: any,y: any, z?: string) => assert.equal(JSON.stringify(x), JSON.stringify(y), z);
|
||||
|
||||
// @deno-types="./types/index.d.ts"
|
||||
import * as X from "./printj.mjs";
|
||||
|
||||
var sprintf = X.sprintf;
|
||||
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_~{|}`".split("");
|
||||
var convs = "aAbBcCdDeEfFgGiJmnoOpsSTuUVxXyY%".split("");
|
||||
var flags = " #'+-0".split("");
|
||||
var digits = "0123456789".split("");
|
||||
var lens = "hIjlLqtwzZ".split(""); lens.push("hh"); lens.push("ll");
|
||||
var other = "$*.".split("");
|
||||
var unused: string[] = []; chars.forEach(function(c) {
|
||||
if(convs.indexOf(c) == -1 &&
|
||||
flags.indexOf(c) == -1 &&
|
||||
digits.indexOf(c) == -1 &&
|
||||
lens.indexOf(c) == -1 &&
|
||||
other.indexOf(c) == -1
|
||||
) unused.push(c);
|
||||
});
|
||||
|
||||
Deno.test('special cases', async function(t) {
|
||||
await t.step('fails on unrecognized format chars: ' + unused.join(""), function() {
|
||||
unused.forEach(function(c) {
|
||||
assert.throws(function() { sprintf("%" + c, 0); }, "Should fail on %" +c);
|
||||
});
|
||||
});
|
||||
await t.step('accepts all expected conversions: ' + convs.join(""), function() {
|
||||
convs.forEach(function(c) {
|
||||
assert.doesNotThrow(function() { sprintf("%" + c, 0); }, "Should pass on %" +c);
|
||||
});
|
||||
});
|
||||
await t.step('accepts all expected lengths: ' + lens.join(""), function() {
|
||||
lens.forEach(function(l) {
|
||||
var fmt = "%" + l + "X";
|
||||
assert.doesNotThrow(function() { sprintf(fmt, 0); }, "Should pass on "+fmt);
|
||||
});
|
||||
});
|
||||
await t.step('accepts all expected flags: ' + flags.join(""), function() {
|
||||
flags.forEach(function(l) {
|
||||
var fmt = "%" + l + "X";
|
||||
assert.doesNotThrow(function() { sprintf(fmt, 0); }, "Should pass on "+fmt);
|
||||
});
|
||||
});
|
||||
await t.step('correctly handles character conversions: cC', function() {
|
||||
assert.equal(sprintf("|%c %c|", "69", 69), "|6 E|");
|
||||
assert.equal(sprintf("|%c|", {toString:function() { return "69"; }, valueOf: function() { return 69; }} ), "|6|");
|
||||
});
|
||||
await t.step('correctly handles error conversion: m', function() {
|
||||
var x = new Error("sheetjs");
|
||||
(x as any).errno = 69; x.toString = function() { return "SHEETJS"; };
|
||||
assert.equal(sprintf("|%#m|", x), "|sheetjs|");
|
||||
delete (x as any).message;
|
||||
assert.equal(sprintf("|%#m|", x), "|Error number 69|");
|
||||
delete (x as any).errno;
|
||||
assert.equal(sprintf("|%#m|", x), "|Error SHEETJS|");
|
||||
});
|
||||
await t.step('correctly handles typeof and valueOf conversions: TV', function() {
|
||||
assert.equal(sprintf("%1$T %1$#T", 1), 'number Number');
|
||||
assert.equal(sprintf("%1$T %1$#T", 'foo'), 'string String');
|
||||
assert.equal(sprintf("%1$T %1$#T", [1,2,3]), 'object Array');
|
||||
assert.equal(sprintf("%1$T %1$#T", null).replace(/Object|global/, "Null"), 'object Null');
|
||||
assert.equal(sprintf("%1$T %1$#T", undefined).replace(/Object|global/, "Undefined"), 'undefined Undefined');
|
||||
|
||||
var _f = function() { return "f"; };
|
||||
var _3 = function() { return 3; };
|
||||
assert.equal(sprintf("%1$d %1$s %1$V", {toString:_f}), '0 f f');
|
||||
assert.equal(sprintf("%1$d %1$s %1$V", {valueOf:_3}), '3 [object Object] 3');
|
||||
assert.equal(sprintf("%1$d %1$s %1$V", {valueOf:_3, toString:_f}), '3 f 3');
|
||||
});
|
||||
await t.step('correctly handles standard integer conversions: diouxXDUO', function() {
|
||||
assert.equal(sprintf("%02hhx %02hhX", 1, 1234321), "01 91");
|
||||
assert.equal(sprintf("%02hhx %-02hhX", -1, -253), "ff 3 ");
|
||||
assert.equal(sprintf("%#02llx", -3), "0xfffffffffffffffd");
|
||||
assert.equal(sprintf("%#02llX", -3), "0XFFFFFFFFFFFFFFFD");
|
||||
assert.equal(sprintf("%#02llo", -3), "01777777777777777777775");
|
||||
assert.equal(sprintf("%#02llu", -3), "18446744073709551613");
|
||||
assert.equal(sprintf("%#03lld", -3), "-03");
|
||||
assert.equal(sprintf("%.9d %.9d", 123456, -123456), "000123456 -000123456");
|
||||
});
|
||||
await t.step('correctly handles new binary conversions: bB', function() {
|
||||
assert.equal(sprintf("%#b", -3), "0b11111111111111111111111111111101");
|
||||
assert.equal(sprintf("%#5B", 3), " 0b11");
|
||||
});
|
||||
await t.step('recognizes IEEE754 special values', function() {
|
||||
assert.equal(sprintf("%a", Infinity), "inf");
|
||||
assert.equal(sprintf("%e", -Infinity), "-inf");
|
||||
assert.equal(sprintf("%f", 0/0), "nan");
|
||||
assert.equal(sprintf("%g", 1/-Infinity), "-0");
|
||||
});
|
||||
await t.step('correctly handles floating point conversions: aAeEfFgG', function() {
|
||||
assert.equal(sprintf("%1$g %1$#g", 1e5), "100000 100000.");
|
||||
assert.equal(sprintf("%.3g %.3g", 1.2345e-4, 1.2345e-5), "0.000123 1.23e-05");
|
||||
assert.equal(sprintf("%f", 1.23e22).replace("10486","00000"), "12300000000000000000000.000000");
|
||||
assert.equal(sprintf("|%1$4.1f|%1$04.1f|%1$-4.1f", 1.2), "| 1.2|01.2|1.2 ");
|
||||
assert.equal(sprintf("%1$.1a|%1$04.0f", -128), "-0x1.0p+7|-128");
|
||||
assert.equal(sprintf("%1$.1a|%1$04.0f", -6.9e-11), "-0x1.3p-34|-000");
|
||||
assert.equal(sprintf("%a %A %a %A", 1, .2, .69, 6e20), "0x1p+0 0X1.999999999999AP-3 0x1.6147ae147ae14p-1 0X1.043561A88293P+69");
|
||||
assert.equal(sprintf("%La %LA %La %LA", 1, .2, .69, 6e20), "0x8p-3 0XC.CCCCCCCCCCCDP-6 0xb.0a3d70a3d70ap-4 0X8.21AB0D441498P+66");
|
||||
assert.equal(sprintf("%010.1a", 1.), "0x001.0p+0");
|
||||
assert.equal(sprintf("%.7a %.7a", 129, -129), "0x1.0200000p+7 -0x1.0200000p+7");
|
||||
assert.equal(sprintf("%.7a", -3.1), "-0x1.8cccccdp+1");
|
||||
});
|
||||
await t.step('consistently handles null and undefined', function() {
|
||||
assert.equal(sprintf("|%1$a|%1$A|%1$e|%1$E|%1$f|%1$F|%1$g|%1$G|", undefined), "|nan|NAN|nan|NAN|nan|NAN|nan|NAN|");
|
||||
assert.equal(sprintf("|%1$a|%1$A|%1$e|%1$E|%1$f|%1$F|%1$g|%1$G|", null), "|nan|NAN|nan|NAN|nan|NAN|nan|NAN|");
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", undefined), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", null), "|0|0|0|0|0|0|0|0|0|0|0|");
|
||||
});
|
||||
await t.step('correctly handles JSON conversion: J', function() {
|
||||
var data = ({a:1});
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
X.set_inspect(function(data: any) { return "|" + JSON.stringify(data) + "|"; });
|
||||
assert.equal(sprintf("%J", data), JSON.stringify(data));
|
||||
assert.equal(sprintf("%#J", data), "|" + JSON.stringify(data) + "|");
|
||||
});
|
||||
await t.step('handles dynamic specifiers', function() {
|
||||
assert.equal(sprintf("|%5s|", "sheetjs"), "|sheetjs|");
|
||||
assert.equal(sprintf("|%*s|", 5, "sheetjs"), "|sheetjs|");
|
||||
assert.equal(sprintf("|%2$*1$s|", 5, "sheetjs", 10), "|sheetjs|");
|
||||
assert.equal(sprintf("|%10s|", "sheetjs"), "| sheetjs|");
|
||||
assert.equal(sprintf("|%2$*3$s|", 5, "sheetjs", 10), "| sheetjs|");
|
||||
assert.equal(sprintf("|%0*.*d|", 4, 2, 1), "| 01|");
|
||||
assert.equal(sprintf("|%1$0*3$.*2$d|", 1, 2, 4), "| 01|");
|
||||
assert.equal(sprintf("|%*.*d|", 4, 2, 1), "| 01|");
|
||||
assert.equal(sprintf("|%-*.*d|", 4, 2, 1), "|01 |");
|
||||
assert.equal(sprintf("|%*.*d|", -4, 2, 1), "|01 |");
|
||||
assert.equal(sprintf("|%-*.*d|", -4, 2, 1), "|01 |");
|
||||
assert.equal(sprintf("|%*s|", 4, "sheetjs"), "|sheetjs|");
|
||||
assert.equal(sprintf("|%*.*s|", 4, 3, "sheetjs"), "| she|");
|
||||
assert.equal(sprintf("|%*.*s|", 4, 2, "sheetjs"), "| sh|");
|
||||
assert.equal(sprintf("|%*.*s|", 4, 1, "sheetjs"), "| s|");
|
||||
assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |");
|
||||
assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|");
|
||||
});
|
||||
await t.step('handles long strings', function() {
|
||||
for(var i = 10; i <= 1000; i+= 10) assert.equal(sprintf("%" + i + "s", "abc").length, i);
|
||||
});
|
||||
});
|
70
tests/mjs.html
Normal file
70
tests/mjs.html
Normal file
@ -0,0 +1,70 @@
|
||||
<!DOCTYPE html>
|
||||
<!-- printj.js (C) 2016-present SheetJS http://sheetjs.com -->
|
||||
<!-- vim: set ts=2: -->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>PRINTJ Live Demo</title>
|
||||
<style>
|
||||
table, td {
|
||||
border: 1px dotted black;
|
||||
text-align: left;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<b>PRINTJ ESM Live Demo</b><br />
|
||||
<a href="https://github.com/SheetJS/printj">Source Code Repo</a><br />
|
||||
<a href="https://github.com/SheetJS/printj/issues">Issues? Something look weird? Click here and report an issue</a><br />
|
||||
<br />
|
||||
<pre>
|
||||
usage: import { sprintf as printf } from './printj.mjs';
|
||||
|
||||
This implementation supports the full POSIX set of conversions. Consult the enclosed README for full details.
|
||||
</pre>
|
||||
<div><a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html">POSIX documentation</a></div><br />
|
||||
<table id="data">
|
||||
<tr><th>example</th><th>result</th></tr>
|
||||
</table>
|
||||
<script type="module">
|
||||
/* eslint-env browser */
|
||||
/*global PRINTJ */
|
||||
import { sprintf as printf } from './printj.mjs';
|
||||
var table = document.getElementById("data");
|
||||
var ex = [
|
||||
'printf("|Hello %s|", "SheetJS")',
|
||||
'printf("|%1$d %1$o %1$u %1$x %1$X|", 1234)',
|
||||
'printf("|%1$d %1$o %1$u %1$x %1$X|", -1234)',
|
||||
'printf("|%1$d %1$o %1$u %1$x %1$X %1$lld|", 2813308004)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", 1.2)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", Math.PI)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", Math.LOG2E)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", -Math.LOG10E)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", Infinity)',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", -0)',
|
||||
'printf("|%012s|", "SheetJS")',
|
||||
'printf("|%-12s|", "SheetJS")',
|
||||
'printf("|%1$f %1$F %1$e %1$E %1$g %1$G %1$a %1$A|", 1.4142E69)',
|
||||
'printf("%%")'
|
||||
];
|
||||
window.onload = function() {
|
||||
printf("Hello %s", "world");
|
||||
ex.forEach(function(m) {
|
||||
var row = table.insertRow(table.rows.length);
|
||||
var c1 = row.insertCell(0); c1.innerHTML = "<pre>" + m + "</pre>";
|
||||
var c2 = row.insertCell(1); c2.innerHTML = "<pre>" + eval(m) + "</pre>";
|
||||
});
|
||||
};
|
||||
var _gaq = _gaq || [];
|
||||
_gaq.push(['_setAccount', 'UA-36810333-1']);
|
||||
_gaq.push(['_trackPageview']);
|
||||
|
||||
(function() {
|
||||
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
||||
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
||||
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
5
tests/node.mjs
Executable file
5
tests/node.mjs
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env node --experimental-modules
|
||||
|
||||
import { version, sprintf } from '../printj.mjs'
|
||||
|
||||
console.log(sprintf("PRINTJ version %s, 123 = 0x%02hhx", version, 123));
|
1
tests/printj.mjs
Symbolic link
1
tests/printj.mjs
Symbolic link
@ -0,0 +1 @@
|
||||
../printj.mjs
|
@ -1,8 +1,8 @@
|
||||
/* printj.ts (C) 2016-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2 ft=typescript: */
|
||||
/*jshint node:true, evil:true */
|
||||
/// <reference types="node" />
|
||||
import X = require("printj");
|
||||
let argv = ["n:1","a","e:null","f:3.4", "b:true", "e:1+1"];
|
||||
const argv = ["n:1","a","e:null","f:3.4", "b:true", "e:1+1"];
|
||||
|
||||
function help() {
|
||||
[
|
||||
@ -10,6 +10,7 @@ function help() {
|
||||
"",
|
||||
"Options:",
|
||||
" -h, --help output usage information",
|
||||
" -d, --dump print debug information about format string",
|
||||
"",
|
||||
"Arguments are treated as strings unless prefaced by a type indicator:",
|
||||
" n:<integer> call parseInt (ex. n:3 -> 3)",
|
||||
@ -32,22 +33,32 @@ return 0;
|
||||
}
|
||||
|
||||
function parse_arg(arg: string): any {
|
||||
let m: string = arg.substr(2), p: number = 0;
|
||||
const m: string = arg.substr(2);
|
||||
let p = 0;
|
||||
if(arg.charCodeAt(1) === 58) switch((p = arg.charCodeAt(0))) {
|
||||
case /*n*/ 110: return parseInt(m, 10);
|
||||
case /*f*/ 102: return parseFloat(m);
|
||||
case /*b*/ 98: return !(m.toUpperCase() === "FALSE" || m === "0");
|
||||
case /*j*/ 106: return JSON.parse(m);
|
||||
case /*e*/ 101: return /*eval*/(m);
|
||||
case /*s*/ 115: return m;
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
||||
let args: any[] = [];
|
||||
const args: any[] = [];
|
||||
let fmt = "", n = 0;
|
||||
for(let i = 2; i < argv.length; ++i) switch(argv[i]) {
|
||||
case "--help": case "-h": break;
|
||||
case "--help": case "-h": process.exit(help()); break;
|
||||
case "--dump": case "-d": if(fmt.length===0) fmt = argv[++i]; process.exit(dump(fmt)); break;
|
||||
default: if(n++ === 0) fmt = argv[i]; else args.push(parse_arg(argv[i]));
|
||||
}
|
||||
|
||||
console.log(X.vsprintf(fmt, args));
|
||||
process.exit(0);
|
||||
|
||||
function dump(fmt: string): number {
|
||||
if(!fmt) { console.error("printj: missing format argument"); return 1; }
|
||||
// X._tokenize(fmt).forEach(function(x){console.log(x);});
|
||||
return 0;
|
||||
}
|
||||
|
4
types/index.d.ts
vendored
4
types/index.d.ts
vendored
@ -1,5 +1,4 @@
|
||||
/* index.d.ts (C) 2015-present SheetJS */
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
/** Version string */
|
||||
export const version: string;
|
||||
@ -9,3 +8,6 @@ export function sprintf(fmt: string, ...args: any[]): string;
|
||||
|
||||
/** Generate formatted string from format and array of variables */
|
||||
export function vsprintf(fmt: string, args: any[]): string;
|
||||
|
||||
/** Override inspect function for #J conversion */
|
||||
export function set_inspect(inspector: (val: any)=>string): void;
|
@ -4,11 +4,12 @@
|
||||
"lib": [ "es5", "dom" ],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": ".",
|
||||
"paths": { "printj": ["."] },
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"strictFunctionTypes": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
{
|
||||
"extends": "dtslint/dtslint.json",
|
||||
"rules": {
|
||||
"no-implicit-dependencies": false,
|
||||
"whitespace": false,
|
||||
"no-sparse-arrays": false,
|
||||
"only-arrow-functions": false,
|
||||
"no-consecutive-blank-lines": false,
|
||||
"prefer-conditional-expression": false,
|
||||
"prefer-const": true,
|
||||
"one-variable-per-declaration": false
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user