1.0.7 • Published 11 months ago

nv-buf-utf8 v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

nv-buf-utf8

  • nv-buf-utf8
  • a utf8 step-decoder, input one-byte each time. return-code-point-if-success , return-undefined-if-in-waiting-more-bytes
  • support two-modes: auto-recover AND throw-error
  • can observe state : 'INIT', 'U1', 'U2', 'U3', 'U4', 'ERROR'
  • its for remote decode , normally USELESS

install

  • npm install nv-buf-utf8

usage

     const {decd} = require("nv-buf-utf8");
     const {Mach} = decd.mach;
     

example

            var mach = new Mach()

            var buf = Buffer.from("我你");
            /*
            > "我".codePointAt(0)
            25105
            > "你".codePointAt(0)
            20320
            > 
            */

            > mach.input(buf[0])
            undefined
            > mach.input(buf[1])
            undefined
            > mach
            Mach(7) [{"cursor":5,"state":"U3","data":[230,136],"err_byte":0}] [
                5, 19, 0, 230,
              136,  0, 0
            ]
            > mach.input(buf[2])
            25105
            > 
            > mach
            Mach(7) [{"cursor":3,"state":"INIT","data":[],"err_byte":0}] [
              3, 16, 0, 0,
              0,  0, 0
            ]
            > 
            > mach.input(buf[3])
            undefined
            > mach.input(buf[4])
            undefined
            > mach.input(buf[5])
            20320
            > 
            > mach.input(buf[0])
            undefined
            > mach
            Mach(7) [{"cursor":4,"state":"U3","data":[230],"err_byte":0}] [
              4, 19, 0, 230,
              0,  0, 0
            ]
            > mach.lefted_
            [ 230 ]
            > mach.input(buf[1])
            undefined
            > mach.lefted_
            [ 230, 136 ]
            > mach.input(buf[2])
            25105
            > mach.lefted_
            []
            > 

METHODS

decode

            decd.mach.mach.cursor_                  decd.mach.mach.dict
            decd.mach.mach.disable_auto_recover     decd.mach.mach.enable_auto_recover      decd.mach.mach.err_byte_
            decd.mach.mach.input                    decd.mach.mach.is_auto_recover_enabled  decd.mach.mach.is_err
            decd.mach.mach.is_init                  decd.mach.mach.is_u2                    decd.mach.mach.is_u3
            decd.mach.mach.is_u4                    decd.mach.mach.lefted_                  decd.mach.mach.reset
            decd.mach.mach.state_code_              decd.mach.mach.state_name_



             mach: {
                STATES: [ 'INIT', 'U1', 'U2', 'U3', 'U4', 'ERROR' ],
                ERRORS: {
                  in_error_must_reset: 'in_error_must_reset',
                  fst_byte_invalid: 'first byte must be <0xxxxxxx> OR <110xxxxx> OR <1110xxxx> OR <11110xxx>',
                  follow_byte_invalid: 'follow byte must be <10xxxxxx>'
                },
                Mach: [class Mach extends Uint8Array] { ALGN_OFFSET: 3 },
                auto_recover_mach: Mach(7) [{"cursor":3,"state":"INIT","data":[],"err_byte":0}] [
                  3, 16, 0, 0,
                  0,  0, 0
                ],
                mach: Mach(7) [{"cursor":3,"state":"INIT","data":[],"err_byte":0}] [
                  3, 0, 0, 0,
                  0, 0, 0
                ]
              }

API

decode

            > decd.def
            {
              is_u1_head: [Function: is_u1_head],
              is_u2_head: [Function: is_u2_head],
              is_u3_head: [Function: is_u3_head],
              is_u4_head: [Function: is_u4_head],
              is_follow: [Function: is_follow],
              u2_to_code: [Function: u2_to_code],
              u3_to_code: [Function: u3_to_code],
              u4_to_code: [Function: u4_to_code],
              barr_to_code: [Function: barr_to_code]
            }
            >  

LICENSE

  • ISC