[Pkg-javascript-commits] [node-is-utf8] 01/04: Import Upstream version 0.2.1

Shirish Togarla shirish12-guest at moszumanska.debian.org
Tue Feb 7 18:01:13 UTC 2017


This is an automated email from the git hooks/post-receive script.

shirish12-guest pushed a commit to branch master
in repository node-is-utf8.

commit 6508d0e7a3d628bb42db9571e0b235a65014422d
Author: Shirish Togarla <shirishtogarla533 at gmail.com>
Date:   Sat Feb 4 14:47:52 2017 +0000

    Import Upstream version 0.2.1
---
 LICENSE      |  9 +++++++
 README.md    | 16 +++++++++++++
 is-utf8.js   | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json | 19 +++++++++++++++
 4 files changed, 120 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2c8d4b9
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+The MIT License (MIT)
+
+Copyright (C) 2014 Wei Fanzhe
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+  
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b62ddde
--- /dev/null
+++ b/README.md
@@ -0,0 +1,16 @@
+#utf8 detector
+
+Detect if a Buffer is utf8 encoded. 
+It need The minimum amount of bytes is 4.
+
+
+```javascript
+    var fs = require('fs');
+    var isUtf8 = require('is-utf8');
+    var ansi = fs.readFileSync('ansi.txt');
+    var utf8 = fs.readFileSync('utf8.txt');
+    
+    console.log('ansi.txt is utf8: '+isUtf8(ansi)); //false
+    console.log('utf8.txt is utf8: '+isUtf8(utf8)); //true
+```
+    
diff --git a/is-utf8.js b/is-utf8.js
new file mode 100644
index 0000000..8a5f15d
--- /dev/null
+++ b/is-utf8.js
@@ -0,0 +1,76 @@
+
+exports = module.exports = function(bytes)
+{
+    var i = 0;
+    while(i < bytes.length)
+    {
+        if(     (// ASCII
+                    bytes[i] == 0x09 ||
+                    bytes[i] == 0x0A ||
+                    bytes[i] == 0x0D ||
+                    (0x20 <= bytes[i] && bytes[i] <= 0x7E)
+                )
+          ) {
+              i += 1;
+              continue;
+          }
+
+        if(     (// non-overlong 2-byte
+                    (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
+                    (0x80 <= bytes[i+1] && bytes[i+1] <= 0xBF)
+                )
+          ) {
+              i += 2;
+              continue;
+          }
+
+        if(     (// excluding overlongs
+                    bytes[i] == 0xE0 &&
+                    (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+                    (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
+                ) ||
+                (// straight 3-byte
+                 ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
+                  bytes[i] == 0xEE ||
+                  bytes[i] == 0xEF) &&
+                 (0x80 <= bytes[i + 1] && bytes[i+1] <= 0xBF) &&
+                 (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
+                ) ||
+                (// excluding surrogates
+                 bytes[i] == 0xED &&
+                 (0x80 <= bytes[i+1] && bytes[i+1] <= 0x9F) &&
+                 (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
+                )
+          ) {
+              i += 3;
+              continue;
+          }
+
+        if(     (// planes 1-3
+                    bytes[i] == 0xF0 &&
+                    (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+                    (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+                    (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+                ) ||
+                (// planes 4-15
+                 (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
+                 (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+                 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+                 (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+                ) ||
+                (// plane 16
+                 bytes[i] == 0xF4 &&
+                 (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
+                 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+                 (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+                )
+          ) {
+              i += 4;
+              continue;
+          }
+
+        return false;
+    }
+
+    return true;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..dab1234
--- /dev/null
+++ b/package.json
@@ -0,0 +1,19 @@
+{
+  "name": "is-utf8",
+  "version": "0.2.1",
+  "description": "Detect if a buffer is utf8 encoded.",
+  "main": "is-utf8.js",
+  "scripts": {
+    "test": "node test.js"
+  },
+  "repository": "https://github.com/wayfind/is-utf8.git",
+  "keywords": [
+    "utf8",
+    "charset"
+  ],
+  "files": [
+    "is-utf8.js"
+  ],
+  "author": "wayfind",
+  "license": "MIT"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-is-utf8.git



More information about the Pkg-javascript-commits mailing list