File: UnicodeRange.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (166 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download | duplicates (2)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include "precompiled.h"
#include "UnicodeRange.h"

namespace Rocket {
namespace Core {

UnicodeRange::UnicodeRange()
{
	min_codepoint = UINT_MAX;
	max_codepoint = UINT_MAX;
}

UnicodeRange::UnicodeRange(int _min_codepoint, int _max_codepoint)
{
	min_codepoint = _min_codepoint;
	max_codepoint = _max_codepoint;

	ROCKET_ASSERT(min_codepoint <= max_codepoint);
}

// Initialises the range from a unicode range in string form.
bool UnicodeRange::Initialise(const String& unicode_range)
{
	// Check for a 'U+' at the start.
	if (unicode_range.Length() < 2 ||
		unicode_range[0] != 'U' ||
		unicode_range[1] != '+')
		return false;

	// Check if there's a '-' sign; if so, we've got a range.
	String::size_type separator_index = unicode_range.Find("-", 2);
	if (separator_index != String::npos)
	{
		const char* end = unicode_range.CString() + separator_index;
		min_codepoint = strtoul(unicode_range.CString() + 2, (char **) &end, 16);

		end = unicode_range.CString() + unicode_range.Length();
		max_codepoint = strtoul(unicode_range.CString() + separator_index + 1, (char **) &end, 16);

		return min_codepoint <= max_codepoint;
	}

	// No range! Check if we have any wildcards.
	String::size_type wildcard_index = unicode_range.Find("?", 2);
	if (wildcard_index != String::npos)
	{
		String range_min(unicode_range.CString() + 2, unicode_range.CString() + wildcard_index);
		String range_max(range_min);

		for (String::size_type i = 0; i < unicode_range.Length() - wildcard_index; ++i)
		{
			range_min += "0";
			range_max += "F";
		}

		const char* end = range_min.CString() + range_min.Length();
		min_codepoint = strtoul(range_min.CString(), (char**) &end, 16);
		end = range_max.CString() + range_max.Length();
		max_codepoint = strtoul(range_max.CString(), (char**) &end, 16);

		return true;
	}

	const char* end = unicode_range.CString() + unicode_range.Length();
	min_codepoint = strtoul(unicode_range.CString() + 2, (char**) &end, 16);
	max_codepoint = min_codepoint;

	return true;
}

// Builds up a list of unicode ranges from a comma-separated list of unicode ranges in string form.
bool UnicodeRange::BuildList(UnicodeRangeList& list, const String& unicode_range)
{
	StringList unicode_ranges;
	StringUtilities::ExpandString(unicode_ranges, unicode_range);

	for (size_t i = 0; i < unicode_ranges.size(); ++i)
	{
		UnicodeRange range;
		if (!range.Initialise(unicode_ranges[i]))
			return false;

		list.push_back(range);
	}

	// Collapse contiguous ranges.
	for (size_t i = 0; i < list.size(); ++i)
	{
		size_t j = i + 1;
		while (j < list.size())
		{
			if (list[i].IsContiguous(list[j]))
			{
				list[i] = list[i].Join(list[j]);
				list.erase(list.begin() + j);
			}
			else
				++j;
		}
	}

	return !list.empty();
}

// Returns true if this range is wholly contained within another range.
bool UnicodeRange::IsContained(const UnicodeRange& rhs)
{
	return rhs.min_codepoint <= min_codepoint &&
		   rhs.max_codepoint >= max_codepoint;
}

// Returns true if this range is wholly contained within another range list.
bool UnicodeRange::IsContained(const UnicodeRangeList& rhs)
{
	for (size_t i = 0; i < rhs.size(); ++i)
	{
		if (IsContained(rhs[i]))
			return true;
	}

	return false;
}

// Returns true if this range is contained or contiguous with another range.
bool UnicodeRange::IsContiguous(const UnicodeRange& rhs)
{
	return (min_codepoint >= rhs.min_codepoint && min_codepoint <= ((rhs.max_codepoint == 0xFFFFFFFF) ? rhs.max_codepoint : rhs.max_codepoint + 1)) ||
		   (max_codepoint >= ((rhs.min_codepoint == 0) ? 0 : rhs.min_codepoint - 1) && max_codepoint <= rhs.max_codepoint);
}

// Joins this range with another that it is contiguous with.
UnicodeRange UnicodeRange::Join(const UnicodeRange& rhs)
{
	return UnicodeRange(Math::Min(min_codepoint, rhs.min_codepoint),
						   Math::Max(max_codepoint, rhs.max_codepoint));
}

}
}