-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.c
42 lines (35 loc) · 962 Bytes
/
decoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "tokenizer.h"
char *decode(unsigned *string, VocabularyItem *vocabulary);
char *decode(unsigned *string, VocabularyItem *vocabulary)
{
unsigned *stringPtr = string;
char *newString = malloc(sizeof(char) * MAX_LENGTH);
char *newStringPtr = newString;
while (*stringPtr != '\0')
{
unsigned idx = *stringPtr;
if (idx < 256)
{
// Small exclusion of special characters which are part of the text
// I do this because they're anoying to print. e.g. 27 will end the program
// without first converting to an escape sequence (feel free to add if you read this).
if (idx > 31)
{
*newStringPtr = idx;
newStringPtr++;
}
}
else
{
VocabularyItem *vocabularyPtr = &vocabulary[idx];
VocabularyPair pair = vocabularyPtr->vocabularyCharacter.pair;
*newStringPtr = pair.first;
newStringPtr++;
*newStringPtr = pair.second;
newStringPtr++;
}
stringPtr++;
}
*newStringPtr = '\0';
return newString;
}