zoukankan      html  css  js  c++  java
  • JavaScript判断文件是否为UTF-8编码

    function readFileToString(code) {
          const reader = new FileReader();
          reader.onload = function() {
            console.log(this.result);
            let str = this.result
            var v8 = new Uint8Array(this.result);
            //if("gbk"===code){
              str = iconv.decode(v8,code) 
           // } 
            
          };
          reader.onerror = e => {
            reject(e);
          };
          reader.readAsArrayBuffer(file);
        }
        (function() {
          const reader = new FileReader();
          reader.onload = function() {
            var v8 = new Uint8Array(this.result);
            if(isUTF8(v8)){
              readFileToString("utf-8")
            }else{
              readFileToString("gbk")
            }
          };
          reader.onerror = e => {
            reject(e);
          };
          reader.readAsArrayBuffer(file);
        })();
    function isUTF8(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;
    }

     转自:is-utf8/is-utf8.js at master · wayfind/is-utf8 (github.com)

  • 相关阅读:
    一元运算符重载 前置和后置++ --(这种一般用成员函数来实现重载)
    运算符中的二元重载,为什么要调用友元函数而不是全局函数的问题
    关于数组的封装不知道为什么错了,具体代码如下
    关于对象的动态建立和释放
    关于构造函数中调用构造函数的危险
    关于析构函数,构造函数匿名对象的总结,以厚忘了,可以回来观看很全
    关于深拷贝和浅拷贝的疑问
    构造函数的调用大全
    构造函数的调用(其中不包括赋值构造函数)
    LeetCode:Add Digits
  • 原文地址:https://www.cnblogs.com/Leechg/p/14098473.html
Copyright © 2011-2022 走看看