Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package stringprep |
| 8 | |
| 9 | import "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. |
| 13 | type RuneRange [2]rune |
| 14 | |
| 15 | // Contains returns true if a rune is within the bounds of the RuneRange. |
| 16 | func (rr RuneRange) Contains(r rune) bool { |
| 17 | return rr[0] <= r && r <= rr[1] |
| 18 | } |
| 19 | |
| 20 | func (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. |
| 26 | type Set []RuneRange |
| 27 | |
| 28 | // Contains returns true if a rune is within any of the RuneRanges in the |
| 29 | // Set. |
| 30 | func (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 | } |