aboutsummaryrefslogtreecommitdiff
path: root/ucd/api.go
blob: 8265d54b87b5be2363b1c209c9193408278102c0 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:generate go run ../cmd/ucdgen/main.go
//go:generate go fmt codepoint.go

package ucd

import (
	"fmt"
	"strings"
)

const (
	// https://www.unicode.org/versions/Unicode13.0.0/ch03.pdf
	// 3.4  Characters and Encoding
	// > D9 Unicode codespace: A range of integers from 0 to 10FFFF16.
	codePointMin = 0x0
	codePointMax = 0x10FFFF
)

func NormalizeCharacterProperty(propName, propVal string) (string, error) {
	if propName == "" {
		propName = "gc"
	}

	name, ok := propertyNameAbbs[normalizeSymbolicValue(propName)]
	if !ok {
		return "", fmt.Errorf("unsupported character property name: %v", propName)
	}
	props, ok := derivedCoreProperties[name]
	if !ok {
		return "", nil
	}
	var b strings.Builder
	yes, ok := binaryValues[normalizeSymbolicValue(propVal)]
	if !ok {
		return "", fmt.Errorf("unsupported character property value: %v", propVal)
	}
	if yes {
		fmt.Fprint(&b, "[")
	} else {
		fmt.Fprint(&b, "[^")
	}
	for _, prop := range props {
		fmt.Fprint(&b, prop)
	}
	fmt.Fprint(&b, "]")

	return b.String(), nil
}

func IsContributoryProperty(propName string) bool {
	if propName == "" {
		return false
	}

	for _, p := range contributoryProperties {
		if propName == p {
			return true
		}
	}
	return false
}

func FindCodePointRanges(propName, propVal string) ([]*CodePointRange, bool, error) {
	if propName == "" {
		propName = "gc"
	}

	name, ok := propertyNameAbbs[normalizeSymbolicValue(propName)]
	if !ok {
		return nil, false, fmt.Errorf("unsupported character property name: %v", propName)
	}
	switch name {
	case "gc":
		val, ok := generalCategoryValueAbbs[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if val == generalCategoryValueAbbs[normalizeSymbolicValue(generalCategoryDefaultValue)] {
			var allCPs []*CodePointRange
			if generalCategoryDefaultRange.From > codePointMin {
				allCPs = append(allCPs, &CodePointRange{
					From: codePointMin,
					To:   generalCategoryDefaultRange.From - 1,
				})
			}
			if generalCategoryDefaultRange.To < codePointMax {
				allCPs = append(allCPs, &CodePointRange{
					From: generalCategoryDefaultRange.To + 1,
					To:   codePointMax,
				})
			}
			for _, cp := range generalCategoryCodePoints {
				allCPs = append(allCPs, cp...)
			}
			return allCPs, true, nil
		}
		vals, ok := compositGeneralCategories[val]
		if !ok {
			vals = []string{val}
		}
		var ranges []*CodePointRange
		for _, v := range vals {
			rs, ok := generalCategoryCodePoints[v]
			if !ok {
				return nil, false, fmt.Errorf("invalid value of the General_Category property: %v", v)
			}
			ranges = append(ranges, rs...)
		}
		return ranges, false, nil
	case "sc":
		val, ok := scriptValueAbbs[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if val == scriptValueAbbs[normalizeSymbolicValue(scriptDefaultValue)] {
			var allCPs []*CodePointRange
			if scriptDefaultRange.From > codePointMin {
				allCPs = append(allCPs, &CodePointRange{
					From: codePointMin,
					To:   scriptDefaultRange.From - 1,
				})
			}
			if scriptDefaultRange.To < codePointMax {
				allCPs = append(allCPs, &CodePointRange{
					From: scriptDefaultRange.To + 1,
					To:   codePointMax,
				})
			}
			for _, cp := range scriptCodepoints {
				allCPs = append(allCPs, cp...)
			}
			return allCPs, true, nil
		}
		return scriptCodepoints[val], false, nil
	case "oalpha":
		yes, ok := binaryValues[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if yes {
			return otherAlphabeticCodePoints, false, nil
		} else {
			return otherAlphabeticCodePoints, true, nil
		}
	case "olower":
		yes, ok := binaryValues[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if yes {
			return otherLowercaseCodePoints, false, nil
		} else {
			return otherLowercaseCodePoints, true, nil
		}
	case "oupper":
		yes, ok := binaryValues[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if yes {
			return otherUppercaseCodePoints, false, nil
		} else {
			return otherUppercaseCodePoints, true, nil
		}
	case "wspace":
		yes, ok := binaryValues[normalizeSymbolicValue(propVal)]
		if !ok {
			return nil, false, fmt.Errorf("unsupported character property value: %v", propVal)
		}
		if yes {
			return whiteSpaceCodePoints, false, nil
		} else {
			return whiteSpaceCodePoints, true, nil
		}
	}

	// If the process reaches this code, it's a bug. We must handle all of the properties registered with
	// the `propertyNameAbbs`.
	return nil, false, fmt.Errorf("character property '%v' is unavailable", propName)
}