1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
17 11-bit: 110xxxxx 10xxxxxx
18 16-bit: 1110xxxx 10xxxxxx 10xxxxxx
19 21-bit: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
22 26-bit: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
23 31-bit: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
26 /* Number of leading ones before a zero in the byte. */
27 static unsigned char const cm_utf8_ones[256] = {
28 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
29 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
30 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
32 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
33 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
34 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
35 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,7,8
38 /* Mask away control bits from bytes with n leading ones. */
39 static unsigned char const cm_utf8_mask[7] = {
40 0xEF, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01
43 /* Minimum allowed value when first byte has n leading ones. */
44 static unsigned int const cm_utf8_min[7] = {
45 0, 0, 1u<<7, 1u<<11, 1u<<16, 1u<<21, 1u<<26 /*, 1u<<31 */
48 /*--------------------------------------------------------------------------*/
49 const char* cm_utf8_decode_character(const char* first, const char* last,
52 /* Count leading ones in the first byte. */
53 unsigned char c = (unsigned char)*first++;
54 unsigned char const ones = cm_utf8_ones[c];
57 case 0: *pc = c; return first; /* One-byte character. */
58 case 1: case 7: case 8: return 0; /* Invalid leading byte. */
62 /* Extract bits from this multi-byte character. */
64 unsigned int uc = c & cm_utf8_mask[ones];
66 for(left = ones-1; left && first != last; --left)
68 c = (unsigned char)*first++;
69 if(cm_utf8_ones[c] != 1)
73 uc = (uc << 6) | (c & cm_utf8_mask[1]);
76 if(left > 0 || uc < cm_utf8_min[ones])