sassc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +24 -0
  8. data/Rakefile +21 -0
  9. data/ext/libsass/.editorconfig +15 -0
  10. data/ext/libsass/.gitattributes +2 -0
  11. data/ext/libsass/.gitignore +61 -0
  12. data/ext/libsass/.travis.yml +38 -0
  13. data/ext/libsass/COPYING +25 -0
  14. data/ext/libsass/INSTALL +1 -0
  15. data/ext/libsass/LICENSE +25 -0
  16. data/ext/libsass/Makefile +223 -0
  17. data/ext/libsass/Makefile.am +145 -0
  18. data/ext/libsass/Readme.md +93 -0
  19. data/ext/libsass/appveyor.yml +76 -0
  20. data/ext/libsass/ast.cpp +581 -0
  21. data/ext/libsass/ast.hpp +1949 -0
  22. data/ext/libsass/ast_def_macros.hpp +16 -0
  23. data/ext/libsass/ast_factory.hpp +87 -0
  24. data/ext/libsass/ast_fwd_decl.hpp +72 -0
  25. data/ext/libsass/b64/cencode.h +32 -0
  26. data/ext/libsass/b64/encode.h +77 -0
  27. data/ext/libsass/backtrace.hpp +81 -0
  28. data/ext/libsass/base64vlq.cpp +43 -0
  29. data/ext/libsass/base64vlq.hpp +28 -0
  30. data/ext/libsass/bind.cpp +187 -0
  31. data/ext/libsass/bind.hpp +18 -0
  32. data/ext/libsass/cencode.c +102 -0
  33. data/ext/libsass/color_names.hpp +324 -0
  34. data/ext/libsass/configure.ac +130 -0
  35. data/ext/libsass/constants.cpp +144 -0
  36. data/ext/libsass/constants.hpp +145 -0
  37. data/ext/libsass/context.cpp +507 -0
  38. data/ext/libsass/context.hpp +150 -0
  39. data/ext/libsass/contextualize.cpp +157 -0
  40. data/ext/libsass/contextualize.hpp +65 -0
  41. data/ext/libsass/copy_c_str.cpp +13 -0
  42. data/ext/libsass/copy_c_str.hpp +5 -0
  43. data/ext/libsass/debug.hpp +39 -0
  44. data/ext/libsass/environment.hpp +75 -0
  45. data/ext/libsass/error_handling.cpp +28 -0
  46. data/ext/libsass/error_handling.hpp +28 -0
  47. data/ext/libsass/eval.cpp +1149 -0
  48. data/ext/libsass/eval.hpp +80 -0
  49. data/ext/libsass/expand.cpp +430 -0
  50. data/ext/libsass/expand.hpp +77 -0
  51. data/ext/libsass/extconf.rb +6 -0
  52. data/ext/libsass/extend.cpp +1962 -0
  53. data/ext/libsass/extend.hpp +50 -0
  54. data/ext/libsass/file.cpp +291 -0
  55. data/ext/libsass/file.hpp +18 -0
  56. data/ext/libsass/functions.cpp +1565 -0
  57. data/ext/libsass/functions.hpp +187 -0
  58. data/ext/libsass/inspect.cpp +727 -0
  59. data/ext/libsass/inspect.hpp +108 -0
  60. data/ext/libsass/json.cpp +1411 -0
  61. data/ext/libsass/json.hpp +117 -0
  62. data/ext/libsass/kwd_arg_macros.hpp +23 -0
  63. data/ext/libsass/m4/.gitkeep +0 -0
  64. data/ext/libsass/mapping.hpp +17 -0
  65. data/ext/libsass/memory_manager.hpp +54 -0
  66. data/ext/libsass/node.cpp +251 -0
  67. data/ext/libsass/node.hpp +122 -0
  68. data/ext/libsass/operation.hpp +153 -0
  69. data/ext/libsass/output_compressed.cpp +401 -0
  70. data/ext/libsass/output_compressed.hpp +95 -0
  71. data/ext/libsass/output_nested.cpp +364 -0
  72. data/ext/libsass/output_nested.hpp +108 -0
  73. data/ext/libsass/parser.cpp +2016 -0
  74. data/ext/libsass/parser.hpp +264 -0
  75. data/ext/libsass/paths.hpp +69 -0
  76. data/ext/libsass/position.hpp +22 -0
  77. data/ext/libsass/posix/getopt.c +562 -0
  78. data/ext/libsass/posix/getopt.h +95 -0
  79. data/ext/libsass/prelexer.cpp +688 -0
  80. data/ext/libsass/prelexer.hpp +513 -0
  81. data/ext/libsass/remove_placeholders.cpp +59 -0
  82. data/ext/libsass/remove_placeholders.hpp +43 -0
  83. data/ext/libsass/res/resource.rc +35 -0
  84. data/ext/libsass/sass.cpp +33 -0
  85. data/ext/libsass/sass.h +60 -0
  86. data/ext/libsass/sass2scss.cpp +834 -0
  87. data/ext/libsass/sass2scss.h +110 -0
  88. data/ext/libsass/sass_context.cpp +709 -0
  89. data/ext/libsass/sass_context.h +120 -0
  90. data/ext/libsass/sass_functions.cpp +137 -0
  91. data/ext/libsass/sass_functions.h +90 -0
  92. data/ext/libsass/sass_interface.cpp +277 -0
  93. data/ext/libsass/sass_interface.h +97 -0
  94. data/ext/libsass/sass_util.cpp +136 -0
  95. data/ext/libsass/sass_util.hpp +259 -0
  96. data/ext/libsass/sass_values.cpp +337 -0
  97. data/ext/libsass/sass_values.h +124 -0
  98. data/ext/libsass/script/bootstrap +10 -0
  99. data/ext/libsass/script/branding +10 -0
  100. data/ext/libsass/script/ci-build-libsass +72 -0
  101. data/ext/libsass/script/ci-install-compiler +4 -0
  102. data/ext/libsass/script/ci-install-deps +19 -0
  103. data/ext/libsass/script/ci-report-coverage +25 -0
  104. data/ext/libsass/script/coveralls-debug +32 -0
  105. data/ext/libsass/script/spec +5 -0
  106. data/ext/libsass/script/tap-driver +652 -0
  107. data/ext/libsass/script/tap-runner +1 -0
  108. data/ext/libsass/source_map.cpp +133 -0
  109. data/ext/libsass/source_map.hpp +46 -0
  110. data/ext/libsass/subset_map.hpp +145 -0
  111. data/ext/libsass/support/libsass.pc.in +11 -0
  112. data/ext/libsass/test-driver +127 -0
  113. data/ext/libsass/test/test_node.cpp +98 -0
  114. data/ext/libsass/test/test_paths.cpp +29 -0
  115. data/ext/libsass/test/test_selector_difference.cpp +28 -0
  116. data/ext/libsass/test/test_specificity.cpp +28 -0
  117. data/ext/libsass/test/test_subset_map.cpp +472 -0
  118. data/ext/libsass/test/test_superselector.cpp +71 -0
  119. data/ext/libsass/test/test_unification.cpp +33 -0
  120. data/ext/libsass/to_c.cpp +61 -0
  121. data/ext/libsass/to_c.hpp +44 -0
  122. data/ext/libsass/to_string.cpp +29 -0
  123. data/ext/libsass/to_string.hpp +32 -0
  124. data/ext/libsass/token.hpp +32 -0
  125. data/ext/libsass/units.cpp +54 -0
  126. data/ext/libsass/units.hpp +10 -0
  127. data/ext/libsass/utf8.h +34 -0
  128. data/ext/libsass/utf8/checked.h +327 -0
  129. data/ext/libsass/utf8/core.h +329 -0
  130. data/ext/libsass/utf8/unchecked.h +228 -0
  131. data/ext/libsass/utf8_string.cpp +102 -0
  132. data/ext/libsass/utf8_string.hpp +36 -0
  133. data/ext/libsass/util.cpp +189 -0
  134. data/ext/libsass/util.hpp +26 -0
  135. data/ext/libsass/win/libsass.filters +291 -0
  136. data/ext/libsass/win/libsass.sln +28 -0
  137. data/ext/libsass/win/libsass.vcxproj +255 -0
  138. data/lib/sassc.rb +6 -0
  139. data/lib/sassc/engine.rb +13 -0
  140. data/lib/sassc/native.rb +44 -0
  141. data/lib/sassc/native/native_context_api.rb +140 -0
  142. data/lib/sassc/native/native_functions_api.rb +41 -0
  143. data/lib/sassc/native/sass_input_style.rb +11 -0
  144. data/lib/sassc/native/sass_output_style.rb +10 -0
  145. data/lib/sassc/native/sass_value.rb +95 -0
  146. data/lib/sassc/native/string_list.rb +8 -0
  147. data/lib/sassc/version.rb +3 -0
  148. data/sassc.gemspec +43 -0
  149. data/test/smoke_test.rb +171 -0
  150. data/test/test_helper.rb +4 -0
  151. metadata +281 -0
@@ -0,0 +1,329 @@
1
+ // Copyright 2006 Nemanja Trifunovic
2
+
3
+ /*
4
+ Permission is hereby granted, free of charge, to any person or organization
5
+ obtaining a copy of the software and accompanying documentation covered by
6
+ this license (the "Software") to use, reproduce, display, distribute,
7
+ execute, and transmit the Software, and to prepare derivative works of the
8
+ Software, and to permit third-parties to whom the Software is furnished to
9
+ do so, all subject to the following:
10
+
11
+ The copyright notices in the Software and this entire statement, including
12
+ the above license grant, this restriction and the following disclaimer,
13
+ must be included in all copies of the Software, in whole or in part, and
14
+ all derivative works of the Software, unless such copies or derivative
15
+ works are solely in the form of machine-executable object code generated by
16
+ a source language processor.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21
+ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22
+ FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
+ DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+
28
+ #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29
+ #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
30
+
31
+ #include <iterator>
32
+
33
+ namespace utf8
34
+ {
35
+ // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
36
+ // You may need to change them to match your system.
37
+ // These typedefs have the same names as ones from cstdint, or boost/cstdint
38
+ typedef unsigned char uint8_t;
39
+ typedef unsigned short uint16_t;
40
+ typedef unsigned int uint32_t;
41
+
42
+ // Helper code - not intended to be directly called by the library users. May be changed at any time
43
+ namespace internal
44
+ {
45
+ // Unicode constants
46
+ // Leading (high) surrogates: 0xd800 - 0xdbff
47
+ // Trailing (low) surrogates: 0xdc00 - 0xdfff
48
+ const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
49
+ const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
50
+ const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
51
+ const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
52
+ const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
53
+ const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
54
+
55
+ // Maximum valid value for a Unicode code point
56
+ const uint32_t CODE_POINT_MAX = 0x0010ffffu;
57
+
58
+ template<typename octet_type>
59
+ inline uint8_t mask8(octet_type oc)
60
+ {
61
+ return static_cast<uint8_t>(0xff & oc);
62
+ }
63
+ template<typename u16_type>
64
+ inline uint16_t mask16(u16_type oc)
65
+ {
66
+ return static_cast<uint16_t>(0xffff & oc);
67
+ }
68
+ template<typename octet_type>
69
+ inline bool is_trail(octet_type oc)
70
+ {
71
+ return ((utf8::internal::mask8(oc) >> 6) == 0x2);
72
+ }
73
+
74
+ template <typename u16>
75
+ inline bool is_lead_surrogate(u16 cp)
76
+ {
77
+ return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
78
+ }
79
+
80
+ template <typename u16>
81
+ inline bool is_trail_surrogate(u16 cp)
82
+ {
83
+ return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
84
+ }
85
+
86
+ template <typename u16>
87
+ inline bool is_surrogate(u16 cp)
88
+ {
89
+ return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
90
+ }
91
+
92
+ template <typename u32>
93
+ inline bool is_code_point_valid(u32 cp)
94
+ {
95
+ return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
96
+ }
97
+
98
+ template <typename octet_iterator>
99
+ inline typename std::iterator_traits<octet_iterator>::difference_type
100
+ sequence_length(octet_iterator lead_it)
101
+ {
102
+ uint8_t lead = utf8::internal::mask8(*lead_it);
103
+ if (lead < 0x80)
104
+ return 1;
105
+ else if ((lead >> 5) == 0x6)
106
+ return 2;
107
+ else if ((lead >> 4) == 0xe)
108
+ return 3;
109
+ else if ((lead >> 3) == 0x1e)
110
+ return 4;
111
+ else
112
+ return 0;
113
+ }
114
+
115
+ template <typename octet_difference_type>
116
+ inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
117
+ {
118
+ if (cp < 0x80) {
119
+ if (length != 1)
120
+ return true;
121
+ }
122
+ else if (cp < 0x800) {
123
+ if (length != 2)
124
+ return true;
125
+ }
126
+ else if (cp < 0x10000) {
127
+ if (length != 3)
128
+ return true;
129
+ }
130
+
131
+ return false;
132
+ }
133
+
134
+ enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
135
+
136
+ /// Helper for get_sequence_x
137
+ template <typename octet_iterator>
138
+ utf_error increase_safely(octet_iterator& it, octet_iterator end)
139
+ {
140
+ if (++it == end)
141
+ return NOT_ENOUGH_ROOM;
142
+
143
+ if (!utf8::internal::is_trail(*it))
144
+ return INCOMPLETE_SEQUENCE;
145
+
146
+ return UTF8_OK;
147
+ }
148
+
149
+ #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
150
+
151
+ /// get_sequence_x functions decode utf-8 sequences of the length x
152
+ template <typename octet_iterator>
153
+ utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point)
154
+ {
155
+ if (it == end)
156
+ return NOT_ENOUGH_ROOM;
157
+
158
+ code_point = utf8::internal::mask8(*it);
159
+
160
+ return UTF8_OK;
161
+ }
162
+
163
+ template <typename octet_iterator>
164
+ utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point)
165
+ {
166
+ if (it == end)
167
+ return NOT_ENOUGH_ROOM;
168
+
169
+ code_point = utf8::internal::mask8(*it);
170
+
171
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
172
+
173
+ code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
174
+
175
+ return UTF8_OK;
176
+ }
177
+
178
+ template <typename octet_iterator>
179
+ utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point)
180
+ {
181
+ if (it == end)
182
+ return NOT_ENOUGH_ROOM;
183
+
184
+ code_point = utf8::internal::mask8(*it);
185
+
186
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
187
+
188
+ code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
189
+
190
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
191
+
192
+ code_point += (*it) & 0x3f;
193
+
194
+ return UTF8_OK;
195
+ }
196
+
197
+ template <typename octet_iterator>
198
+ utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point)
199
+ {
200
+ if (it == end)
201
+ return NOT_ENOUGH_ROOM;
202
+
203
+ code_point = utf8::internal::mask8(*it);
204
+
205
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
206
+
207
+ code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
208
+
209
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
210
+
211
+ code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
212
+
213
+ UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
214
+
215
+ code_point += (*it) & 0x3f;
216
+
217
+ return UTF8_OK;
218
+ }
219
+
220
+ #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
221
+
222
+ template <typename octet_iterator>
223
+ utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point)
224
+ {
225
+ // Save the original value of it so we can go back in case of failure
226
+ // Of course, it does not make much sense with i.e. stream iterators
227
+ octet_iterator original_it = it;
228
+
229
+ uint32_t cp = 0;
230
+ // Determine the sequence length based on the lead octet
231
+ typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
232
+ const octet_difference_type length = utf8::internal::sequence_length(it);
233
+
234
+ // Get trail octets and calculate the code point
235
+ utf_error err = UTF8_OK;
236
+ switch (length) {
237
+ case 0:
238
+ return INVALID_LEAD;
239
+ case 1:
240
+ err = utf8::internal::get_sequence_1(it, end, cp);
241
+ break;
242
+ case 2:
243
+ err = utf8::internal::get_sequence_2(it, end, cp);
244
+ break;
245
+ case 3:
246
+ err = utf8::internal::get_sequence_3(it, end, cp);
247
+ break;
248
+ case 4:
249
+ err = utf8::internal::get_sequence_4(it, end, cp);
250
+ break;
251
+ }
252
+
253
+ if (err == UTF8_OK) {
254
+ // Decoding succeeded. Now, security checks...
255
+ if (utf8::internal::is_code_point_valid(cp)) {
256
+ if (!utf8::internal::is_overlong_sequence(cp, length)){
257
+ // Passed! Return here.
258
+ code_point = cp;
259
+ ++it;
260
+ return UTF8_OK;
261
+ }
262
+ else
263
+ err = OVERLONG_SEQUENCE;
264
+ }
265
+ else
266
+ err = INVALID_CODE_POINT;
267
+ }
268
+
269
+ // Failure branch - restore the original value of the iterator
270
+ it = original_it;
271
+ return err;
272
+ }
273
+
274
+ template <typename octet_iterator>
275
+ inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
276
+ uint32_t ignored;
277
+ return utf8::internal::validate_next(it, end, ignored);
278
+ }
279
+
280
+ } // namespace internal
281
+
282
+ /// The library API - functions intended to be called by the users
283
+
284
+ // Byte order mark
285
+ const uint8_t bom[] = {0xef, 0xbb, 0xbf};
286
+
287
+ template <typename octet_iterator>
288
+ octet_iterator find_invalid(octet_iterator start, octet_iterator end)
289
+ {
290
+ octet_iterator result = start;
291
+ while (result != end) {
292
+ utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
293
+ if (err_code != internal::UTF8_OK)
294
+ return result;
295
+ }
296
+ return result;
297
+ }
298
+
299
+ template <typename octet_iterator>
300
+ inline bool is_valid(octet_iterator start, octet_iterator end)
301
+ {
302
+ return (utf8::find_invalid(start, end) == end);
303
+ }
304
+
305
+ template <typename octet_iterator>
306
+ inline bool starts_with_bom (octet_iterator it, octet_iterator end)
307
+ {
308
+ return (
309
+ ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
310
+ ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
311
+ ((it != end) && (utf8::internal::mask8(*it)) == bom[2])
312
+ );
313
+ }
314
+
315
+ //Deprecated in release 2.3
316
+ template <typename octet_iterator>
317
+ inline bool is_bom (octet_iterator it)
318
+ {
319
+ return (
320
+ (utf8::internal::mask8(*it++)) == bom[0] &&
321
+ (utf8::internal::mask8(*it++)) == bom[1] &&
322
+ (utf8::internal::mask8(*it)) == bom[2]
323
+ );
324
+ }
325
+ } // namespace utf8
326
+
327
+ #endif // header guard
328
+
329
+
@@ -0,0 +1,228 @@
1
+ // Copyright 2006 Nemanja Trifunovic
2
+
3
+ /*
4
+ Permission is hereby granted, free of charge, to any person or organization
5
+ obtaining a copy of the software and accompanying documentation covered by
6
+ this license (the "Software") to use, reproduce, display, distribute,
7
+ execute, and transmit the Software, and to prepare derivative works of the
8
+ Software, and to permit third-parties to whom the Software is furnished to
9
+ do so, all subject to the following:
10
+
11
+ The copyright notices in the Software and this entire statement, including
12
+ the above license grant, this restriction and the following disclaimer,
13
+ must be included in all copies of the Software, in whole or in part, and
14
+ all derivative works of the Software, unless such copies or derivative
15
+ works are solely in the form of machine-executable object code generated by
16
+ a source language processor.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21
+ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22
+ FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
+ DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+
28
+ #ifndef UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29
+ #define UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
30
+
31
+ #include "core.h"
32
+
33
+ namespace utf8
34
+ {
35
+ namespace unchecked
36
+ {
37
+ template <typename octet_iterator>
38
+ octet_iterator append(uint32_t cp, octet_iterator result)
39
+ {
40
+ if (cp < 0x80) // one octet
41
+ *(result++) = static_cast<uint8_t>(cp);
42
+ else if (cp < 0x800) { // two octets
43
+ *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
44
+ *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
45
+ }
46
+ else if (cp < 0x10000) { // three octets
47
+ *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
48
+ *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
49
+ *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
50
+ }
51
+ else { // four octets
52
+ *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
53
+ *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
54
+ *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
55
+ *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
56
+ }
57
+ return result;
58
+ }
59
+
60
+ template <typename octet_iterator>
61
+ uint32_t next(octet_iterator& it)
62
+ {
63
+ uint32_t cp = utf8::internal::mask8(*it);
64
+ typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
65
+ switch (length) {
66
+ case 1:
67
+ break;
68
+ case 2:
69
+ it++;
70
+ cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
71
+ break;
72
+ case 3:
73
+ ++it;
74
+ cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
75
+ ++it;
76
+ cp += (*it) & 0x3f;
77
+ break;
78
+ case 4:
79
+ ++it;
80
+ cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
81
+ ++it;
82
+ cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
83
+ ++it;
84
+ cp += (*it) & 0x3f;
85
+ break;
86
+ }
87
+ ++it;
88
+ return cp;
89
+ }
90
+
91
+ template <typename octet_iterator>
92
+ uint32_t peek_next(octet_iterator it)
93
+ {
94
+ return utf8::unchecked::next(it);
95
+ }
96
+
97
+ template <typename octet_iterator>
98
+ uint32_t prior(octet_iterator& it)
99
+ {
100
+ while (utf8::internal::is_trail(*(--it))) ;
101
+ octet_iterator temp = it;
102
+ return utf8::unchecked::next(temp);
103
+ }
104
+
105
+ // Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
106
+ template <typename octet_iterator>
107
+ inline uint32_t previous(octet_iterator& it)
108
+ {
109
+ return utf8::unchecked::prior(it);
110
+ }
111
+
112
+ template <typename octet_iterator, typename distance_type>
113
+ void advance (octet_iterator& it, distance_type n)
114
+ {
115
+ for (distance_type i = 0; i < n; ++i)
116
+ utf8::unchecked::next(it);
117
+ }
118
+
119
+ template <typename octet_iterator>
120
+ typename std::iterator_traits<octet_iterator>::difference_type
121
+ distance (octet_iterator first, octet_iterator last)
122
+ {
123
+ typename std::iterator_traits<octet_iterator>::difference_type dist;
124
+ for (dist = 0; first < last; ++dist)
125
+ utf8::unchecked::next(first);
126
+ return dist;
127
+ }
128
+
129
+ template <typename u16bit_iterator, typename octet_iterator>
130
+ octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
131
+ {
132
+ while (start != end) {
133
+ uint32_t cp = utf8::internal::mask16(*start++);
134
+ // Take care of surrogate pairs first
135
+ if (utf8::internal::is_lead_surrogate(cp)) {
136
+ uint32_t trail_surrogate = utf8::internal::mask16(*start++);
137
+ cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
138
+ }
139
+ result = utf8::unchecked::append(cp, result);
140
+ }
141
+ return result;
142
+ }
143
+
144
+ template <typename u16bit_iterator, typename octet_iterator>
145
+ u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
146
+ {
147
+ while (start < end) {
148
+ uint32_t cp = utf8::unchecked::next(start);
149
+ if (cp > 0xffff) { //make a surrogate pair
150
+ *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
151
+ *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
152
+ }
153
+ else
154
+ *result++ = static_cast<uint16_t>(cp);
155
+ }
156
+ return result;
157
+ }
158
+
159
+ template <typename octet_iterator, typename u32bit_iterator>
160
+ octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
161
+ {
162
+ while (start != end)
163
+ result = utf8::unchecked::append(*(start++), result);
164
+
165
+ return result;
166
+ }
167
+
168
+ template <typename octet_iterator, typename u32bit_iterator>
169
+ u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
170
+ {
171
+ while (start < end)
172
+ (*result++) = utf8::unchecked::next(start);
173
+
174
+ return result;
175
+ }
176
+
177
+ // The iterator class
178
+ template <typename octet_iterator>
179
+ class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
180
+ octet_iterator it;
181
+ public:
182
+ iterator () {}
183
+ explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
184
+ // the default "big three" are OK
185
+ octet_iterator base () const { return it; }
186
+ uint32_t operator * () const
187
+ {
188
+ octet_iterator temp = it;
189
+ return utf8::unchecked::next(temp);
190
+ }
191
+ bool operator == (const iterator& rhs) const
192
+ {
193
+ return (it == rhs.it);
194
+ }
195
+ bool operator != (const iterator& rhs) const
196
+ {
197
+ return !(operator == (rhs));
198
+ }
199
+ iterator& operator ++ ()
200
+ {
201
+ ::std::advance(it, utf8::internal::sequence_length(it));
202
+ return *this;
203
+ }
204
+ iterator operator ++ (int)
205
+ {
206
+ iterator temp = *this;
207
+ ::std::advance(it, utf8::internal::sequence_length(it));
208
+ return temp;
209
+ }
210
+ iterator& operator -- ()
211
+ {
212
+ utf8::unchecked::prior(it);
213
+ return *this;
214
+ }
215
+ iterator operator -- (int)
216
+ {
217
+ iterator temp = *this;
218
+ utf8::unchecked::prior(it);
219
+ return temp;
220
+ }
221
+ }; // class iterator
222
+
223
+ } // namespace utf8::unchecked
224
+ } // namespace utf8
225
+
226
+
227
+ #endif // header guard
228
+