Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #ifndef nsCodingStateMachine_h__ |
michael@0 | 6 | #define nsCodingStateMachine_h__ |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/ArrayUtils.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "nsPkgInt.h" |
michael@0 | 11 | |
michael@0 | 12 | typedef enum { |
michael@0 | 13 | eStart = 0, |
michael@0 | 14 | eError = 1, |
michael@0 | 15 | eItsMe = 2 |
michael@0 | 16 | } nsSMState; |
michael@0 | 17 | |
michael@0 | 18 | #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) |
michael@0 | 19 | |
michael@0 | 20 | //state machine model |
michael@0 | 21 | typedef struct |
michael@0 | 22 | { |
michael@0 | 23 | nsPkgInt classTable; |
michael@0 | 24 | uint32_t classFactor; |
michael@0 | 25 | nsPkgInt stateTable; |
michael@0 | 26 | const uint32_t* charLenTable; |
michael@0 | 27 | #ifdef DEBUG |
michael@0 | 28 | const size_t charLenTableLength; |
michael@0 | 29 | #endif |
michael@0 | 30 | const char* name; |
michael@0 | 31 | } SMModel; |
michael@0 | 32 | |
michael@0 | 33 | class nsCodingStateMachine { |
michael@0 | 34 | public: |
michael@0 | 35 | nsCodingStateMachine(const SMModel* sm) : mModel(sm) { mCurrentState = eStart; } |
michael@0 | 36 | nsSMState NextState(char c){ |
michael@0 | 37 | //for each byte we get its class , if it is first byte, we also get byte length |
michael@0 | 38 | uint32_t byteCls = GETCLASS(c); |
michael@0 | 39 | if (mCurrentState == eStart) |
michael@0 | 40 | { |
michael@0 | 41 | mCurrentBytePos = 0; |
michael@0 | 42 | MOZ_ASSERT(byteCls < mModel->charLenTableLength); |
michael@0 | 43 | mCurrentCharLen = mModel->charLenTable[byteCls]; |
michael@0 | 44 | } |
michael@0 | 45 | //from byte's class and stateTable, we get its next state |
michael@0 | 46 | mCurrentState=(nsSMState)GETFROMPCK(mCurrentState*(mModel->classFactor)+byteCls, |
michael@0 | 47 | mModel->stateTable); |
michael@0 | 48 | mCurrentBytePos++; |
michael@0 | 49 | return mCurrentState; |
michael@0 | 50 | } |
michael@0 | 51 | uint32_t GetCurrentCharLen(void) {return mCurrentCharLen;} |
michael@0 | 52 | void Reset(void) {mCurrentState = eStart;} |
michael@0 | 53 | const char * GetCodingStateMachine() {return mModel->name;} |
michael@0 | 54 | |
michael@0 | 55 | protected: |
michael@0 | 56 | nsSMState mCurrentState; |
michael@0 | 57 | uint32_t mCurrentCharLen; |
michael@0 | 58 | uint32_t mCurrentBytePos; |
michael@0 | 59 | |
michael@0 | 60 | const SMModel *mModel; |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | extern const SMModel UTF8SMModel; |
michael@0 | 64 | extern const SMModel Big5SMModel; |
michael@0 | 65 | extern const SMModel EUCJPSMModel; |
michael@0 | 66 | extern const SMModel EUCKRSMModel; |
michael@0 | 67 | extern const SMModel EUCTWSMModel; |
michael@0 | 68 | extern const SMModel GB18030SMModel; |
michael@0 | 69 | extern const SMModel SJISSMModel; |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | extern const SMModel HZSMModel; |
michael@0 | 73 | extern const SMModel ISO2022CNSMModel; |
michael@0 | 74 | extern const SMModel ISO2022JPSMModel; |
michael@0 | 75 | extern const SMModel ISO2022KRSMModel; |
michael@0 | 76 | |
michael@0 | 77 | #undef CHAR_LEN_TABLE |
michael@0 | 78 | #ifdef DEBUG |
michael@0 | 79 | #define CHAR_LEN_TABLE(x) x, mozilla::ArrayLength(x) |
michael@0 | 80 | #else |
michael@0 | 81 | #define CHAR_LEN_TABLE(x) x |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | #endif /* nsCodingStateMachine_h__ */ |
michael@0 | 85 |