1 | // sass.hpp must go before all system headers to get the |
---|---|
2 | // __EXTENSIONS__ fix on Solaris. |
3 | #include "sass.hpp" |
4 | |
5 | #include "base64vlq.hpp" |
6 | |
7 | namespace Sass { |
8 | |
9 | sass::string Base64VLQ::encode(const int number) const |
10 | { |
11 | sass::string encoded = ""; |
12 | |
13 | int vlq = to_vlq_signed(number); |
14 | |
15 | do { |
16 | int digit = vlq & VLQ_BASE_MASK; |
17 | vlq >>= VLQ_BASE_SHIFT; |
18 | if (vlq > 0) { |
19 | digit |= VLQ_CONTINUATION_BIT; |
20 | } |
21 | encoded += base64_encode(number: digit); |
22 | } while (vlq > 0); |
23 | |
24 | return encoded; |
25 | } |
26 | |
27 | char Base64VLQ::base64_encode(const int number) const |
28 | { |
29 | int index = number; |
30 | if (index < 0) index = 0; |
31 | if (index > 63) index = 63; |
32 | return CHARACTERS[index]; |
33 | } |
34 | |
35 | int Base64VLQ::to_vlq_signed(const int number) const |
36 | { |
37 | return (number < 0) ? ((-number) << 1) + 1 : (number << 1) + 0; |
38 | } |
39 | |
40 | const char* Base64VLQ::CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
41 | |
42 | const int Base64VLQ::VLQ_BASE_SHIFT = 5; |
43 | const int Base64VLQ::VLQ_BASE = 1 << VLQ_BASE_SHIFT; |
44 | const int Base64VLQ::VLQ_BASE_MASK = VLQ_BASE - 1; |
45 | const int Base64VLQ::VLQ_CONTINUATION_BIT = VLQ_BASE; |
46 | |
47 | } |
48 |