-
Notifications
You must be signed in to change notification settings - Fork 1
/
str_iter.cpp
77 lines (64 loc) · 1.67 KB
/
str_iter.cpp
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
// Copyright (c) 2016 Martin Ridgers
// License: http://opensource.org/licenses/MIT
#include "main.h"
#include "str_iter.h"
//------------------------------------------------------------------------------
template <>
int32 str_iter_impl<char>::next()
{
if (!more())
return 0;
int32 ax = 0;
int32 encode_length = 0;
while (int32 c = uint8(*m_ptr++))
{
ax = (ax << 6) | (c & 0x7f);
if (encode_length)
{
--encode_length;
continue;
}
if ((c & 0xc0) < 0xc0)
return ax;
if (encode_length = !!(c & 0x20))
encode_length += !!(c & 0x10);
ax &= (0x1f >> encode_length);
if (!more())
break;
}
return 0;
}
//------------------------------------------------------------------------------
template <>
int32 str_iter_impl<wchar_t>::next()
{
if (!more())
return 0;
int32 ax = 0;
while (int32 c = *m_ptr++)
{
// Decode surrogate pairs.
if ((c & 0xfc00) == 0xd800)
{
ax = c << 10;
continue;
}
else if ((c & 0xfc00) == 0xdc00 && ax >= (1 << 10))
return ax + c - 0x35fdc00;
else
return c;
}
return 0;
}
//------------------------------------------------------------------------------
template <>
uint32 str_iter_impl<char>::length() const
{
return (uint32)((m_ptr <= m_end) ? m_end - m_ptr : strlen(m_ptr));
}
//------------------------------------------------------------------------------
template <>
uint32 str_iter_impl<wchar_t>::length() const
{
return (uint32)((m_ptr <= m_end) ? m_end - m_ptr : wcslen(m_ptr));
}