/
Range.cs
89 lines (80 loc) · 2.74 KB
/
Range.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace NeoSmart.Unicode
{
public class Range
{
public readonly Codepoint Begin;
public readonly Codepoint End;
public Range(Codepoint begin, Codepoint end)
{
Begin = begin;
End = end;
}
public Range(Codepoint value)
{
Begin = value;
End = value;
}
public bool Contains(Codepoint codepoint)
{
return codepoint >= Begin && codepoint <= End;
}
//either a single hex codepoint or two separated by a hyphen
public Range(string range)
{
var values = range.Split(new [] { "-", "–", "—", ".." }, StringSplitOptions.RemoveEmptyEntries); //these are all different hyphens used on Wikipedia and in the UTR
Begin = UInt32.Parse(values[0], System.Globalization.NumberStyles.HexNumber);
if (values.Length == 1)
{
End = Begin;
}
else if (values.Length == 2)
{
End = UInt32.Parse(values[1], System.Globalization.NumberStyles.HexNumber);
}
else
{
throw new InvalidRangeException();
}
}
public IEnumerable<UInt32> AsUtf32Sequence
{
get
{
for (UInt32 i = 0; Begin + i <= End; ++i)
{
yield return new Codepoint(Begin + i).AsUtf32();
}
}
}
public IEnumerable<UInt16> AsUtf16Sequence
{
get
{
for (var i = 0; Begin + i <= End; ++i)
{
foreach (var utf16 in new Codepoint(Begin + i).AsUtf16())
{
yield return utf16;
}
}
}
}
public IEnumerable<byte> AsUtf8Sequence
{
get
{
for (var i = 0; Begin + i <= End; ++i)
{
foreach (var utf8 in new Codepoint(Begin + i).AsUtf8())
{
yield return utf8;
}
}
}
}
}
}