|
3 | 3 | namespace MagesScriptTool; |
4 | 4 |
|
5 | 5 | sealed class CompiledStringEncoding { |
| 6 | + readonly CompiledStringUnitEncoding _unitEncoding; |
6 | 7 | readonly StringTagsSpec _spec; |
7 | 8 |
|
8 | | - public CompiledStringEncoding(StringTagsSpec spec) { |
| 9 | + public CompiledStringEncoding(CompiledStringUnitEncoding unitEncoding, StringTagsSpec spec) { |
| 10 | + _unitEncoding = unitEncoding; |
9 | 11 | _spec = spec; |
10 | 12 | } |
11 | 13 |
|
12 | 14 | public void Encode(Stream stream, ImmutableArray<StringToken> tokens) { |
13 | | - new CompiledStringEncoder(stream, _spec, tokens).Encode(); |
| 15 | + new CompiledStringEncoder(stream, _unitEncoding, _spec, tokens).Encode(); |
14 | 16 | } |
15 | 17 |
|
16 | 18 | public ImmutableArray<StringToken> Decode(Stream stream) { |
17 | | - return new CompiledStringDecoder(stream, _spec).Decode(); |
| 19 | + return new CompiledStringDecoder(stream, _unitEncoding, _spec).Decode(); |
18 | 20 | } |
19 | 21 |
|
20 | 22 | sealed class CompiledStringEncoder { |
21 | 23 | readonly Stream _stream; |
| 24 | + readonly CompiledStringUnitEncoding _unitEncoding; |
22 | 25 | readonly StringTagsSpec _spec; |
23 | 26 | readonly ImmutableArray<StringToken> _tokens; |
24 | 27 |
|
25 | | - public CompiledStringEncoder(Stream stream, StringTagsSpec spec, ImmutableArray<StringToken> tokens) { |
| 28 | + public CompiledStringEncoder(Stream stream, CompiledStringUnitEncoding unitEncoding, StringTagsSpec spec, ImmutableArray<StringToken> tokens) { |
26 | 29 | _stream = stream; |
| 30 | + _unitEncoding = unitEncoding; |
27 | 31 | _spec = spec; |
28 | 32 | _tokens = tokens; |
29 | 33 | } |
@@ -64,8 +68,23 @@ void EncodeTag(StringTokenTag tag) { |
64 | 68 |
|
65 | 69 | void EncodeUnit(StringTokenUnit unit) { |
66 | 70 | int value = unit.Value; |
67 | | - PutByte((byte)((value >> 8) & 0x7F | 0x80)); |
68 | | - PutByte((byte)((value >> 0) & 0xFF)); |
| 71 | + switch (_unitEncoding) { |
| 72 | + case CompiledStringUnitEncoding.UInt16: { |
| 73 | + PutByte((byte)((value >> 8) & 0x7F | 0x80)); |
| 74 | + PutByte((byte)((value >> 0) & 0xFF)); |
| 75 | + break; |
| 76 | + } |
| 77 | + case CompiledStringUnitEncoding.UInt32: { |
| 78 | + PutByte((byte)((value >> 24) & 0x7F | 0x80)); |
| 79 | + PutByte((byte)((value >> 16) & 0xFF)); |
| 80 | + PutByte((byte)((value >> 8) & 0xFF)); |
| 81 | + PutByte((byte)((value >> 0) & 0xFF)); |
| 82 | + break; |
| 83 | + } |
| 84 | + default: { |
| 85 | + throw new NotImplementedException($"{_unitEncoding} is not implemented."); |
| 86 | + } |
| 87 | + } |
69 | 88 | } |
70 | 89 |
|
71 | 90 | void EncodeOperand(OperandKind kind, ExpressionNode expression) { |
@@ -107,10 +126,12 @@ void PutByte(byte value) { |
107 | 126 |
|
108 | 127 | sealed class CompiledStringDecoder { |
109 | 128 | readonly Stream _stream; |
| 129 | + readonly CompiledStringUnitEncoding _unitEncoding; |
110 | 130 | readonly StringTagsSpec _spec; |
111 | 131 |
|
112 | | - public CompiledStringDecoder(Stream stream, StringTagsSpec spec) { |
| 132 | + public CompiledStringDecoder(Stream stream, CompiledStringUnitEncoding unitEncoding, StringTagsSpec spec) { |
113 | 133 | _stream = stream; |
| 134 | + _unitEncoding = unitEncoding; |
114 | 135 | _spec = spec; |
115 | 136 | } |
116 | 137 |
|
@@ -154,8 +175,23 @@ StringTokenTag DecodeTag() { |
154 | 175 |
|
155 | 176 | StringTokenUnit DecodeUnit() { |
156 | 177 | int value = 0; |
157 | | - value |= (GetByte() & 0x7F) << 8; |
158 | | - value |= (GetByte() & 0xFF) << 0; |
| 178 | + switch (_unitEncoding) { |
| 179 | + case CompiledStringUnitEncoding.UInt16: { |
| 180 | + value |= (GetByte() & 0x7F) << 8; |
| 181 | + value |= (GetByte() & 0xFF) << 0; |
| 182 | + break; |
| 183 | + } |
| 184 | + case CompiledStringUnitEncoding.UInt32: { |
| 185 | + value |= (GetByte() & 0x7F) << 24; |
| 186 | + value |= (GetByte() & 0xFF) << 16; |
| 187 | + value |= (GetByte() & 0xFF) << 8; |
| 188 | + value |= (GetByte() & 0xFF) << 0; |
| 189 | + break; |
| 190 | + } |
| 191 | + default: { |
| 192 | + throw new NotImplementedException($"{_unitEncoding} is not implemented."); |
| 193 | + } |
| 194 | + }; |
159 | 195 | return new(value); |
160 | 196 | } |
161 | 197 |
|
|
0 commit comments