-
Notifications
You must be signed in to change notification settings - Fork 22
/
proquint.c
108 lines (86 loc) · 2.44 KB
/
proquint.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This file is part of proquint: http://github.com/dsw/proquint .
See License.txt for copyright and terms of use. */
/*
Convert between proquint, hex, and decimal strings.
Please see the article on proquints: http://arXiv.org/html/0901.4016
Daniel S. Wilkerson
*/
#include "proquint.h"
/* Map uints to consonants. */
static char const uint2consonant[] = {
'b', 'd', 'f', 'g',
'h', 'j', 'k', 'l',
'm', 'n', 'p', 'r',
's', 't', 'v', 'z'
};
/* Map uints to vowels. */
static char const uint2vowel[] = {
'a', 'i', 'o', 'u'
};
void uint2quint(char *quint /*output*/, uint32_t i, int sepChar) {
/* K&R section 2.9: "Right shifting an unsigned quantity always
fills vacated it with zero." */
uint32_t j;
# define APPEND(X) *quint = (X); ++quint
# define MASK_FIRST4 0xF0000000
# define MASK_FIRST2 0xC0000000
# define HANDLE_CONSONANT \
j = i & MASK_FIRST4; \
i <<= 4; j >>= 28; \
APPEND(uint2consonant[j])
# define HANDLE_VOWEL \
j = i & MASK_FIRST2; \
i <<= 2; j >>= 30; \
APPEND(uint2vowel[j])
HANDLE_CONSONANT;
HANDLE_VOWEL;
HANDLE_CONSONANT;
HANDLE_VOWEL;
HANDLE_CONSONANT;
if (sepChar) {
APPEND(((char) sepChar));
}
HANDLE_CONSONANT;
HANDLE_VOWEL;
HANDLE_CONSONANT;
HANDLE_VOWEL;
HANDLE_CONSONANT;
# undef HANDLE_VOWEL
# undef HANDLE_CONSONANT
# undef APPEND
# undef MASK_FIRST2
# undef MASK_FIRST4
}
uint32_t quint2uint(char const *quint) {
uint32_t res = 0;
char c;
for(; (c=*quint); ++quint) {
switch(c) {
/* consonants */
case 'b': res <<= 4; res += 0; break;
case 'd': res <<= 4; res += 1; break;
case 'f': res <<= 4; res += 2; break;
case 'g': res <<= 4; res += 3; break;
case 'h': res <<= 4; res += 4; break;
case 'j': res <<= 4; res += 5; break;
case 'k': res <<= 4; res += 6; break;
case 'l': res <<= 4; res += 7; break;
case 'm': res <<= 4; res += 8; break;
case 'n': res <<= 4; res += 9; break;
case 'p': res <<= 4; res += 10; break;
case 'r': res <<= 4; res += 11; break;
case 's': res <<= 4; res += 12; break;
case 't': res <<= 4; res += 13; break;
case 'v': res <<= 4; res += 14; break;
case 'z': res <<= 4; res += 15; break;
/* vowels */
case 'a': res <<= 2; res += 0; break;
case 'i': res <<= 2; res += 1; break;
case 'o': res <<= 2; res += 2; break;
case 'u': res <<= 2; res += 3; break;
/* separators */
default: break;
}
}
return res;
}