blob: c837e28c88a37987879bb54938d14e00c79f098f [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright 2018 by David A. Golden. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package stringprep
8
9import "sort"
10
11// RuneRange represents a close-ended range of runes: [N,M]. For a range
12// consisting of a single rune, N and M will be equal.
13type RuneRange [2]rune
14
15// Contains returns true if a rune is within the bounds of the RuneRange.
16func (rr RuneRange) Contains(r rune) bool {
17 return rr[0] <= r && r <= rr[1]
18}
19
20func (rr RuneRange) isAbove(r rune) bool {
21 return r <= rr[0]
22}
23
24// Set represents a stringprep data table used to identify runes of a
25// particular type.
26type Set []RuneRange
27
28// Contains returns true if a rune is within any of the RuneRanges in the
29// Set.
30func (s Set) Contains(r rune) bool {
31 i := sort.Search(len(s), func(i int) bool { return s[i].Contains(r) || s[i].isAbove(r) })
32 if i < len(s) && s[i].Contains(r) {
33 return true
34 }
35 return false
36}