| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include <urlan.h> |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | int ur_encodingCharSize[ UR_ENC_COUNT ] = { 1, 1, 2 }; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | int copyLatin1ToUtf8( uint8_t* dest, const uint8_t* src, int len ) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | const uint8_t* start; |
|---|
| 37 | const uint8_t* end; |
|---|
| 38 | uint16_t c; |
|---|
| 39 | |
|---|
| 40 | start = dest; |
|---|
| 41 | end = src + len; |
|---|
| 42 | |
|---|
| 43 | while( src != end ) |
|---|
| 44 | { |
|---|
| 45 | c = *src++; |
|---|
| 46 | if( c > 127 ) |
|---|
| 47 | { |
|---|
| 48 | *dest++ = 0xC0 | (c >> 6); |
|---|
| 49 | c = 0x80 | (c & 0x3f); |
|---|
| 50 | } |
|---|
| 51 | *dest++ = c; |
|---|
| 52 | } |
|---|
| 53 | return dest - start; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | int copyUcs2ToUtf8( uint8_t* dest, const uint16_t* src, int len ) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | const uint8_t* start; |
|---|
| 64 | const uint16_t* end; |
|---|
| 65 | uint16_t c; |
|---|
| 66 | |
|---|
| 67 | start = dest; |
|---|
| 68 | end = src + len; |
|---|
| 69 | |
|---|
| 70 | while( src != end ) |
|---|
| 71 | { |
|---|
| 72 | c = *src++; |
|---|
| 73 | if( c > 127 ) |
|---|
| 74 | { |
|---|
| 75 | if( c > 0x07ff ) |
|---|
| 76 | { |
|---|
| 77 | *dest++ = 0xE0 | (c >> 12); |
|---|
| 78 | *dest++ = 0x80 | ((c >> 6) & 0x3f); |
|---|
| 79 | c = 0x80 | (c & 0x3f); |
|---|
| 80 | } |
|---|
| 81 | else |
|---|
| 82 | { |
|---|
| 83 | *dest++ = 0xC0 | (c >> 6); |
|---|
| 84 | c = 0x80 | (c & 0x3f); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | *dest++ = c; |
|---|
| 88 | } |
|---|
| 89 | return dest - start; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | #if 0 |
|---|
| 94 | int copyUcs2ToAscii( char* dest, const uint16_t* src, int len ) |
|---|
| 95 | { |
|---|
| 96 | const uint16_t* end; |
|---|
| 97 | uint16_t c; |
|---|
| 98 | |
|---|
| 99 | end = src + len; |
|---|
| 100 | |
|---|
| 101 | while( src != end ) |
|---|
| 102 | { |
|---|
| 103 | c = *src++; |
|---|
| 104 | if( c > 127 ) |
|---|
| 105 | c = 0; |
|---|
| 106 | *dest++ = (char) c; |
|---|
| 107 | } |
|---|
| 108 | return len; |
|---|
| 109 | } |
|---|
| 110 | #endif |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | int copyAsciiToUtf16( uint16_t* dest, const uint8_t* src, int len ) |
|---|
| 117 | { |
|---|
| 118 | const uint8_t* end = src + len; |
|---|
| 119 | while( src != end ) |
|---|
| 120 | *dest++ = *src++; |
|---|
| 121 | return len; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | UR_CALL( uc_encode ) |
|---|
| 127 | { |
|---|
| 128 | UIndex strN; |
|---|
| 129 | UCell* res; |
|---|
| 130 | uint8_t* cpA; |
|---|
| 131 | uint8_t* cpB; |
|---|
| 132 | UBinary* bin; |
|---|
| 133 | int count; |
|---|
| 134 | int enc; |
|---|
| 135 | |
|---|
| 136 | UR_S_DROP; |
|---|
| 137 | res = UR_TOS; |
|---|
| 138 | |
|---|
| 139 | if( ur_isAWord(tos) && ur_stringSlice(ut, res, &cpA, &cpB) ) |
|---|
| 140 | { |
|---|
| 141 | count = cpB - cpA; |
|---|
| 142 | |
|---|
| 143 | switch( ur_atom(tos) ) |
|---|
| 144 | { |
|---|
| 145 | case UR_ATOM_LATIN1: |
|---|
| 146 | switch( ur_encoding(res) ) |
|---|
| 147 | { |
|---|
| 148 | case UR_ENC_UCS2: |
|---|
| 149 | count >>= 1; |
|---|
| 150 | strN = ur_makeBinary( count * 2, &bin ); |
|---|
| 151 | bin->used = copyUcs2ToUtf8( bin->ptr.b, |
|---|
| 152 | (uint16_t*) cpA, count ); |
|---|
| 153 | enc = UR_ENC_LATIN1; |
|---|
| 154 | goto set_result; |
|---|
| 155 | } |
|---|
| 156 | break; |
|---|
| 157 | |
|---|
| 158 | case UR_ATOM_UTF8: |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | break; |
|---|
| 171 | |
|---|
| 172 | case UR_ATOM_UCS2: |
|---|
| 173 | switch( ur_encoding(res) ) |
|---|
| 174 | { |
|---|
| 175 | case UR_ENC_LATIN1: |
|---|
| 176 | strN = ur_makeBinary( count * 2, &bin ); |
|---|
| 177 | bin->used = copyAsciiToUtf16( bin->ptr.u16, |
|---|
| 178 | cpA, count ); |
|---|
| 179 | enc = UR_ENC_UCS2; |
|---|
| 180 | goto set_result; |
|---|
| 181 | } |
|---|
| 182 | break; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | return; |
|---|
| 186 | |
|---|
| 187 | set_result: |
|---|
| 188 | |
|---|
| 189 | ur_initType(res, UT_STRING); |
|---|
| 190 | ur_setEncoding(res, enc); |
|---|
| 191 | ur_setSeries(res, strN, 0); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | UR_CALL( uc_encodingQ ) |
|---|
| 197 | { |
|---|
| 198 | UR_CALL_UNUSED_TH |
|---|
| 199 | |
|---|
| 200 | if( ur_is(tos, UT_STRING) ) |
|---|
| 201 | { |
|---|
| 202 | UAtom atom = UR_ATOM_LATIN1; |
|---|
| 203 | |
|---|
| 204 | switch( ur_encoding(tos) ) |
|---|
| 205 | { |
|---|
| 206 | |
|---|
| 207 | case UR_ENC_UTF8: atom = UR_ATOM_UTF8; break; |
|---|
| 208 | case UR_ENC_UCS2: atom = UR_ATOM_UCS2; break; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | ur_initType(tos, UT_WORD); |
|---|
| 212 | ur_setUnbound(tos, atom); |
|---|
| 213 | } |
|---|
| 214 | else |
|---|
| 215 | { |
|---|
| 216 | ur_initType(tos, UT_NONE); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|