Skip to content

Commit f390349

Browse files
committed
Simplify schema by using XXZ[0-9] instead of XXZ[A-J] for leading digits
1 parent 050508e commit f390349

File tree

7 files changed

+47
-148
lines changed

7 files changed

+47
-148
lines changed

Diff for: Elm/DoubleXEncode.elm

+1-78
Original file line numberDiff line numberDiff line change
@@ -232,80 +232,6 @@ charDecode char =
232232
'\u{0000}'
233233

234234

235-
hexDigitEncode : Char -> Char
236-
hexDigitEncode digit =
237-
case digit of
238-
'0' ->
239-
'A'
240-
241-
'1' ->
242-
'B'
243-
244-
'2' ->
245-
'C'
246-
247-
'3' ->
248-
'D'
249-
250-
'4' ->
251-
'E'
252-
253-
'5' ->
254-
'F'
255-
256-
'6' ->
257-
'G'
258-
259-
'7' ->
260-
'H'
261-
262-
'8' ->
263-
'I'
264-
265-
'9' ->
266-
'J'
267-
268-
_ ->
269-
'\u{0000}'
270-
271-
272-
hexDigitDecode : Char -> Char
273-
hexDigitDecode digit =
274-
case digit of
275-
'A' ->
276-
'0'
277-
278-
'B' ->
279-
'1'
280-
281-
'C' ->
282-
'2'
283-
284-
'D' ->
285-
'3'
286-
287-
'E' ->
288-
'4'
289-
290-
'F' ->
291-
'5'
292-
293-
'G' ->
294-
'6'
295-
296-
'H' ->
297-
'7'
298-
299-
'I' ->
300-
'8'
301-
302-
'J' ->
303-
'9'
304-
305-
_ ->
306-
'\u{0000}'
307-
308-
309235
hexShiftEncode : Char -> Char
310236
hexShiftEncode digit =
311237
case digit of
@@ -482,7 +408,7 @@ doubleXEncodeWithOptions encodeOptions string =
482408
|> String.fromList
483409

484410
encodeDigit digit =
485-
String.fromList [ 'X', 'X', 'Z', hexDigitEncode digit ]
411+
String.fromList [ 'X', 'X', 'Z', digit ]
486412
in
487413
if encodeOptions.encodeLeadingDigit then
488414
case String.toList string of
@@ -573,9 +499,6 @@ doubleXDecode text =
573499
( noXX
574500
|> String.dropLeft 1
575501
|> String.left 1
576-
|> String.toList
577-
|> List.map hexDigitDecode
578-
|> String.fromList
579502
, noXX |> String.dropLeft 2
580503
)
581504

Diff for: Elm/Test.elm

+15-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ main =
3535
expectEncodeGql a b =
3636
getEncoderTest doubleXEncodeGql a b
3737

38+
expectDecode a b =
39+
if doubleXDecode a /= b then
40+
"❌ DECODE ERROR: "
41+
++ a
42+
++ " | "
43+
++ doubleXDecode a
44+
++ " /= "
45+
++ b
46+
47+
else
48+
"" ++ a ++ ": " ++ doubleXDecode a ++ " == " ++ b
49+
3850
blnsFiltered =
3951
blns
4052
|> List.filter
@@ -80,7 +92,9 @@ main =
8092
, expectEncode "Multi Byte Emoji: 👨\u{200D}🦲"
8193
"MultiXX0ByteXX0EmojiXXGXX0XXbpegiXXacaanXXbpjlc"
8294
, expectEncodeGql "__Schema" "XXRXXRSchema"
83-
, expectEncodeGql "0test" "XXZAtest"
95+
, expectEncodeGql "also__middle" "alsoXXRXXRmiddle"
96+
, expectEncodeGql "0test" "XXZ0test"
97+
, expectDecode "alsoXXZ0middle" "also0middle"
8498
, "========== ENCODE - DECODE =========="
8599
, blnsFiltered |> List.map testEncodeDecode |> String.join ""
86100
, "========== ENCODE GQL - DECODE =========="

Diff for: Haskell/DoubleXEncode.hs

+1-36
Original file line numberDiff line numberDiff line change
@@ -109,38 +109,6 @@ charDecode = \case
109109
_ -> '\0'
110110

111111

112-
hexDigitEncode :: Char -> Char
113-
hexDigitEncode = \case
114-
'0' -> 'A'
115-
'1' -> 'B'
116-
'2' -> 'C'
117-
'3' -> 'D'
118-
'4' -> 'E'
119-
'5' -> 'F'
120-
'6' -> 'G'
121-
'7' -> 'H'
122-
'8' -> 'I'
123-
'9' -> 'J'
124-
125-
_ -> '\0'
126-
127-
128-
hexDigitDecode :: Char -> Char
129-
hexDigitDecode = \case
130-
'A' -> '0'
131-
'B' -> '1'
132-
'C' -> '2'
133-
'D' -> '3'
134-
'E' -> '4'
135-
'F' -> '5'
136-
'G' -> '6'
137-
'H' -> '7'
138-
'I' -> '8'
139-
'J' -> '9'
140-
141-
_ -> '\0'
142-
143-
144112
hexShiftEncode :: Char -> Char
145113
hexShiftEncode = \case
146114
'0' -> 'a'
@@ -220,7 +188,7 @@ doubleXEncodeWithOptions encodeOptions text =
220188
& T.pack
221189

222190
encodeDigit digit =
223-
T.pack ['X', 'X', 'Z', hexDigitEncode digit]
191+
T.pack ['X', 'X', 'Z', digit]
224192
in
225193
if encodeOptions&encodeLeadingDigit
226194
then
@@ -306,9 +274,6 @@ doubleXDecode text =
306274
(noXX
307275
& T.drop 1
308276
& T.take 1
309-
& T.unpack
310-
<&> hexDigitDecode
311-
& T.pack
312277
, T.drop 2 noXX
313278
)
314279

Diff for: Haskell/Test.hs

+14-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ main = do
3838
expectEncodeGql a b =
3939
getEncoderTest doubleXEncodeGql a b
4040

41+
expectDecode a b = T.putStrLn $
42+
if doubleXDecode a /= b
43+
then
44+
"❌ DECODE ERROR: "
45+
<> a <> " | " <> doubleXDecode a <> " /= " <> b
46+
else
47+
"" <> a <> ": " <> doubleXDecode a <> " == " <> b
48+
4149
expectEncode "camelCaseId" "camelCaseId"
4250
expectEncode "snake_case_id" "snake_case_id"
4351
expectEncode "__Schema" "__Schema"
@@ -55,7 +63,9 @@ main = do
5563
"MultiXX0ByteXX0EmojiXXGXX0XXbpegiXXacaanXXbpjlc"
5664

5765
expectEncodeGql "__Schema" "XXRXXRSchema"
58-
expectEncodeGql "0test" "XXZAtest"
66+
expectEncodeGql "also__middle" "alsoXXRXXRmiddle"
67+
expectEncodeGql "0test" "XXZ0test"
68+
expectDecode "alsoXXZ0middle" "also0middle"
5969

6070
-- https://github.com/minimaxir/big-list-of-naughty-strings
6171
txtFile <- readFile "Haskell/blns.txt"
@@ -85,10 +95,12 @@ main = do
8595
<> "\""
8696
else ""
8797

88-
T.putStrLn "\n\n========== ENCODE - DECODE =========="
98+
T.putStrLn "\n\n============ ENCODE - DECODE ============"
8999
blnsFiltered <&> testEncodeDecode <&> T.putStr & sequence_
90100

91101
T.putStrLn "\n\n========== ENCODE GQL - DECODE =========="
92102
blnsFiltered <&> testEncodeGqlDecode <&> T.putStr & sequence_
93103

104+
T.putStrLn "\n\n============== HASKELL END ==============\n\n"
105+
94106
pure ()

Diff for: JavaScript/index.ts

+4-24
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,6 @@ const charDecode = Object.fromEntries(
4848
.map(([k, v]) => [v, k])
4949
)
5050

51-
const hexDigitEncode = {
52-
"0": "A",
53-
"1": "B",
54-
"2": "C",
55-
"3": "D",
56-
"4": "E",
57-
"5": "F",
58-
"6": "G",
59-
"7": "H",
60-
"8": "I",
61-
"9": "J",
62-
}
63-
64-
const hexDigitDecode = Object.fromEntries(
65-
Object
66-
.entries(hexDigitEncode)
67-
.map(([k, v]) => [v, k])
68-
)
69-
7051
const hexShiftEncode = {
7152
"0": "a",
7253
"1": "b",
@@ -102,9 +83,8 @@ export function doubleXEncode (
10283
): string {
10384
if (options.encodeLeadingDigit) {
10485
const firstChar = str.slice(0, 1)
105-
const firstDigitHex = hexDigitEncode[firstChar]
106-
const firstCharEncoded = firstDigitHex
107-
? "XXZ" + firstDigitHex
86+
const firstCharEncoded = /^[0-9]$/.test(firstChar)
87+
? "XXZ" + firstChar
10888
: firstChar
10989

11090
options.encodeLeadingDigit = false
@@ -148,7 +128,7 @@ export function doubleXDecode (str: string): string {
148128
const strNorm = str.replaceAll("XXXXXX", "XXYXXY")
149129

150130
return strNorm
151-
.split(/(XXY|XXZ[A-J]|XX[0-9A-W]|XX[a-p]{5})/)
131+
.split(/(XXY|XXZ[0-9]|XX[0-9A-W]|XX[a-p]{5})/)
152132
.filter(Boolean) // Remove empty strings
153133
.map(word =>
154134
word.startsWith("XX")
@@ -164,7 +144,7 @@ export function doubleXDecode (str: string): string {
164144
)
165145
)
166146
: (word.slice(2, 3) == "Z")
167-
? hexDigitDecode[word.slice(3, 4)]
147+
? word.slice(3, 4)
168148
: charDecode[word.slice(2, 3)]
169149
)
170150
: word

Diff for: makefile

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
.PHONY: help
2+
help: makefile
3+
@tail -n +4 makefile | grep ".PHONY"
4+
5+
6+
.PHONY: test
7+
test: test-elm test-haskell test-javascript
8+
9+
110
.PHONY: test-elm
211
test-elm:
312
cd Elm && elm make Test.elm --output=elm-tests.js
@@ -10,8 +19,4 @@ test-haskell:
1019

1120
.PHONY: test-javascript
1221
test-javascript:
13-
deno run JavaScript/test.ts
14-
15-
16-
.PHONY: test
17-
test: test-elm test-haskell test-javascript
22+
deno --unstable run JavaScript/test.ts

Diff for: readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ With encoding of leading digit and double underscore activated
3535

3636
Input | Output
3737
------|-------
38-
`1FileFormat` | `XXZAFileFormat`
38+
`1FileFormat` | `XXZ1FileFormat`
3939
`__index__` | `XXRXXRindexXXRXXR`
4040

4141

@@ -53,7 +53,7 @@ The encoding scheme is based on the following rules:
5353
`a` to `p` instead of `0` to `f`.
5454

5555
If the optional leading digit encoding is enabled,
56-
a leading digit is encoded as `XXZ[A-J]`, where `A` is `0` and `J` is `9`.
56+
a leading digit is encoded as `XXZ[0-9]`.
5757

5858
If the optional double underscore encoding is enabled,
5959
double underscores are encoded as `XXRXXR`.

0 commit comments

Comments
 (0)