idna9.0.0.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
  2. // Copyright 2016 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. //go:build !go1.10
  6. // +build !go1.10
  7. // Package idna implements IDNA2008 using the compatibility processing
  8. // defined by UTS (Unicode Technical Standard) #46, which defines a standard to
  9. // deal with the transition from IDNA2003.
  10. //
  11. // IDNA2008 (Internationalized Domain Names for Applications), is defined in RFC
  12. // 5890, RFC 5891, RFC 5892, RFC 5893 and RFC 5894.
  13. // UTS #46 is defined in https://www.unicode.org/reports/tr46.
  14. // See https://unicode.org/cldr/utility/idna.jsp for a visualization of the
  15. // differences between these two standards.
  16. package idna // import "golang.org/x/net/idna"
  17. import (
  18. "fmt"
  19. "strings"
  20. "unicode/utf8"
  21. "golang.org/x/text/secure/bidirule"
  22. "golang.org/x/text/unicode/norm"
  23. )
  24. // NOTE: Unlike common practice in Go APIs, the functions will return a
  25. // sanitized domain name in case of errors. Browsers sometimes use a partially
  26. // evaluated string as lookup.
  27. // TODO: the current error handling is, in my opinion, the least opinionated.
  28. // Other strategies are also viable, though:
  29. // Option 1) Return an empty string in case of error, but allow the user to
  30. // specify explicitly which errors to ignore.
  31. // Option 2) Return the partially evaluated string if it is itself a valid
  32. // string, otherwise return the empty string in case of error.
  33. // Option 3) Option 1 and 2.
  34. // Option 4) Always return an empty string for now and implement Option 1 as
  35. // needed, and document that the return string may not be empty in case of
  36. // error in the future.
  37. // I think Option 1 is best, but it is quite opinionated.
  38. // ToASCII is a wrapper for Punycode.ToASCII.
  39. func ToASCII(s string) (string, error) {
  40. return Punycode.process(s, true)
  41. }
  42. // ToUnicode is a wrapper for Punycode.ToUnicode.
  43. func ToUnicode(s string) (string, error) {
  44. return Punycode.process(s, false)
  45. }
  46. // An Option configures a Profile at creation time.
  47. type Option func(*options)
  48. // Transitional sets a Profile to use the Transitional mapping as defined in UTS
  49. // #46. This will cause, for example, "ß" to be mapped to "ss". Using the
  50. // transitional mapping provides a compromise between IDNA2003 and IDNA2008
  51. // compatibility. It is used by some browsers when resolving domain names. This
  52. // option is only meaningful if combined with MapForLookup.
  53. func Transitional(transitional bool) Option {
  54. return func(o *options) { o.transitional = transitional }
  55. }
  56. // VerifyDNSLength sets whether a Profile should fail if any of the IDN parts
  57. // are longer than allowed by the RFC.
  58. //
  59. // This option corresponds to the VerifyDnsLength flag in UTS #46.
  60. func VerifyDNSLength(verify bool) Option {
  61. return func(o *options) { o.verifyDNSLength = verify }
  62. }
  63. // RemoveLeadingDots removes leading label separators. Leading runes that map to
  64. // dots, such as U+3002 IDEOGRAPHIC FULL STOP, are removed as well.
  65. func RemoveLeadingDots(remove bool) Option {
  66. return func(o *options) { o.removeLeadingDots = remove }
  67. }
  68. // ValidateLabels sets whether to check the mandatory label validation criteria
  69. // as defined in Section 5.4 of RFC 5891. This includes testing for correct use
  70. // of hyphens ('-'), normalization, validity of runes, and the context rules.
  71. // In particular, ValidateLabels also sets the CheckHyphens and CheckJoiners flags
  72. // in UTS #46.
  73. func ValidateLabels(enable bool) Option {
  74. return func(o *options) {
  75. // Don't override existing mappings, but set one that at least checks
  76. // normalization if it is not set.
  77. if o.mapping == nil && enable {
  78. o.mapping = normalize
  79. }
  80. o.trie = trie
  81. o.checkJoiners = enable
  82. o.checkHyphens = enable
  83. if enable {
  84. o.fromPuny = validateFromPunycode
  85. } else {
  86. o.fromPuny = nil
  87. }
  88. }
  89. }
  90. // CheckHyphens sets whether to check for correct use of hyphens ('-') in
  91. // labels. Most web browsers do not have this option set, since labels such as
  92. // "r3---sn-apo3qvuoxuxbt-j5pe" are in common use.
  93. //
  94. // This option corresponds to the CheckHyphens flag in UTS #46.
  95. func CheckHyphens(enable bool) Option {
  96. return func(o *options) { o.checkHyphens = enable }
  97. }
  98. // CheckJoiners sets whether to check the ContextJ rules as defined in Appendix
  99. // A of RFC 5892, concerning the use of joiner runes.
  100. //
  101. // This option corresponds to the CheckJoiners flag in UTS #46.
  102. func CheckJoiners(enable bool) Option {
  103. return func(o *options) {
  104. o.trie = trie
  105. o.checkJoiners = enable
  106. }
  107. }
  108. // StrictDomainName limits the set of permissable ASCII characters to those
  109. // allowed in domain names as defined in RFC 1034 (A-Z, a-z, 0-9 and the
  110. // hyphen). This is set by default for MapForLookup and ValidateForRegistration,
  111. // but is only useful if ValidateLabels is set.
  112. //
  113. // This option is useful, for instance, for browsers that allow characters
  114. // outside this range, for example a '_' (U+005F LOW LINE). See
  115. // http://www.rfc-editor.org/std/std3.txt for more details.
  116. //
  117. // This option corresponds to the UseSTD3ASCIIRules flag in UTS #46.
  118. func StrictDomainName(use bool) Option {
  119. return func(o *options) { o.useSTD3Rules = use }
  120. }
  121. // NOTE: the following options pull in tables. The tables should not be linked
  122. // in as long as the options are not used.
  123. // BidiRule enables the Bidi rule as defined in RFC 5893. Any application
  124. // that relies on proper validation of labels should include this rule.
  125. //
  126. // This option corresponds to the CheckBidi flag in UTS #46.
  127. func BidiRule() Option {
  128. return func(o *options) { o.bidirule = bidirule.ValidString }
  129. }
  130. // ValidateForRegistration sets validation options to verify that a given IDN is
  131. // properly formatted for registration as defined by Section 4 of RFC 5891.
  132. func ValidateForRegistration() Option {
  133. return func(o *options) {
  134. o.mapping = validateRegistration
  135. StrictDomainName(true)(o)
  136. ValidateLabels(true)(o)
  137. VerifyDNSLength(true)(o)
  138. BidiRule()(o)
  139. }
  140. }
  141. // MapForLookup sets validation and mapping options such that a given IDN is
  142. // transformed for domain name lookup according to the requirements set out in
  143. // Section 5 of RFC 5891. The mappings follow the recommendations of RFC 5894,
  144. // RFC 5895 and UTS 46. It does not add the Bidi Rule. Use the BidiRule option
  145. // to add this check.
  146. //
  147. // The mappings include normalization and mapping case, width and other
  148. // compatibility mappings.
  149. func MapForLookup() Option {
  150. return func(o *options) {
  151. o.mapping = validateAndMap
  152. StrictDomainName(true)(o)
  153. ValidateLabels(true)(o)
  154. RemoveLeadingDots(true)(o)
  155. }
  156. }
  157. type options struct {
  158. transitional bool
  159. useSTD3Rules bool
  160. checkHyphens bool
  161. checkJoiners bool
  162. verifyDNSLength bool
  163. removeLeadingDots bool
  164. trie *idnaTrie
  165. // fromPuny calls validation rules when converting A-labels to U-labels.
  166. fromPuny func(p *Profile, s string) error
  167. // mapping implements a validation and mapping step as defined in RFC 5895
  168. // or UTS 46, tailored to, for example, domain registration or lookup.
  169. mapping func(p *Profile, s string) (string, error)
  170. // bidirule, if specified, checks whether s conforms to the Bidi Rule
  171. // defined in RFC 5893.
  172. bidirule func(s string) bool
  173. }
  174. // A Profile defines the configuration of a IDNA mapper.
  175. type Profile struct {
  176. options
  177. }
  178. func apply(o *options, opts []Option) {
  179. for _, f := range opts {
  180. f(o)
  181. }
  182. }
  183. // New creates a new Profile.
  184. //
  185. // With no options, the returned Profile is the most permissive and equals the
  186. // Punycode Profile. Options can be passed to further restrict the Profile. The
  187. // MapForLookup and ValidateForRegistration options set a collection of options,
  188. // for lookup and registration purposes respectively, which can be tailored by
  189. // adding more fine-grained options, where later options override earlier
  190. // options.
  191. func New(o ...Option) *Profile {
  192. p := &Profile{}
  193. apply(&p.options, o)
  194. return p
  195. }
  196. // ToASCII converts a domain or domain label to its ASCII form. For example,
  197. // ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
  198. // ToASCII("golang") is "golang". If an error is encountered it will return
  199. // an error and a (partially) processed result.
  200. func (p *Profile) ToASCII(s string) (string, error) {
  201. return p.process(s, true)
  202. }
  203. // ToUnicode converts a domain or domain label to its Unicode form. For example,
  204. // ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
  205. // ToUnicode("golang") is "golang". If an error is encountered it will return
  206. // an error and a (partially) processed result.
  207. func (p *Profile) ToUnicode(s string) (string, error) {
  208. pp := *p
  209. pp.transitional = false
  210. return pp.process(s, false)
  211. }
  212. // String reports a string with a description of the profile for debugging
  213. // purposes. The string format may change with different versions.
  214. func (p *Profile) String() string {
  215. s := ""
  216. if p.transitional {
  217. s = "Transitional"
  218. } else {
  219. s = "NonTransitional"
  220. }
  221. if p.useSTD3Rules {
  222. s += ":UseSTD3Rules"
  223. }
  224. if p.checkHyphens {
  225. s += ":CheckHyphens"
  226. }
  227. if p.checkJoiners {
  228. s += ":CheckJoiners"
  229. }
  230. if p.verifyDNSLength {
  231. s += ":VerifyDNSLength"
  232. }
  233. return s
  234. }
  235. var (
  236. // Punycode is a Profile that does raw punycode processing with a minimum
  237. // of validation.
  238. Punycode *Profile = punycode
  239. // Lookup is the recommended profile for looking up domain names, according
  240. // to Section 5 of RFC 5891. The exact configuration of this profile may
  241. // change over time.
  242. Lookup *Profile = lookup
  243. // Display is the recommended profile for displaying domain names.
  244. // The configuration of this profile may change over time.
  245. Display *Profile = display
  246. // Registration is the recommended profile for checking whether a given
  247. // IDN is valid for registration, according to Section 4 of RFC 5891.
  248. Registration *Profile = registration
  249. punycode = &Profile{}
  250. lookup = &Profile{options{
  251. transitional: true,
  252. removeLeadingDots: true,
  253. useSTD3Rules: true,
  254. checkHyphens: true,
  255. checkJoiners: true,
  256. trie: trie,
  257. fromPuny: validateFromPunycode,
  258. mapping: validateAndMap,
  259. bidirule: bidirule.ValidString,
  260. }}
  261. display = &Profile{options{
  262. useSTD3Rules: true,
  263. removeLeadingDots: true,
  264. checkHyphens: true,
  265. checkJoiners: true,
  266. trie: trie,
  267. fromPuny: validateFromPunycode,
  268. mapping: validateAndMap,
  269. bidirule: bidirule.ValidString,
  270. }}
  271. registration = &Profile{options{
  272. useSTD3Rules: true,
  273. verifyDNSLength: true,
  274. checkHyphens: true,
  275. checkJoiners: true,
  276. trie: trie,
  277. fromPuny: validateFromPunycode,
  278. mapping: validateRegistration,
  279. bidirule: bidirule.ValidString,
  280. }}
  281. // TODO: profiles
  282. // Register: recommended for approving domain names: don't do any mappings
  283. // but rather reject on invalid input. Bundle or block deviation characters.
  284. )
  285. type labelError struct{ label, code_ string }
  286. func (e labelError) code() string { return e.code_ }
  287. func (e labelError) Error() string {
  288. return fmt.Sprintf("idna: invalid label %q", e.label)
  289. }
  290. type runeError rune
  291. func (e runeError) code() string { return "P1" }
  292. func (e runeError) Error() string {
  293. return fmt.Sprintf("idna: disallowed rune %U", e)
  294. }
  295. // process implements the algorithm described in section 4 of UTS #46,
  296. // see https://www.unicode.org/reports/tr46.
  297. func (p *Profile) process(s string, toASCII bool) (string, error) {
  298. var err error
  299. if p.mapping != nil {
  300. s, err = p.mapping(p, s)
  301. }
  302. // Remove leading empty labels.
  303. if p.removeLeadingDots {
  304. for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
  305. }
  306. }
  307. // It seems like we should only create this error on ToASCII, but the
  308. // UTS 46 conformance tests suggests we should always check this.
  309. if err == nil && p.verifyDNSLength && s == "" {
  310. err = &labelError{s, "A4"}
  311. }
  312. labels := labelIter{orig: s}
  313. for ; !labels.done(); labels.next() {
  314. label := labels.label()
  315. if label == "" {
  316. // Empty labels are not okay. The label iterator skips the last
  317. // label if it is empty.
  318. if err == nil && p.verifyDNSLength {
  319. err = &labelError{s, "A4"}
  320. }
  321. continue
  322. }
  323. if strings.HasPrefix(label, acePrefix) {
  324. u, err2 := decode(label[len(acePrefix):])
  325. if err2 != nil {
  326. if err == nil {
  327. err = err2
  328. }
  329. // Spec says keep the old label.
  330. continue
  331. }
  332. labels.set(u)
  333. if err == nil && p.fromPuny != nil {
  334. err = p.fromPuny(p, u)
  335. }
  336. if err == nil {
  337. // This should be called on NonTransitional, according to the
  338. // spec, but that currently does not have any effect. Use the
  339. // original profile to preserve options.
  340. err = p.validateLabel(u)
  341. }
  342. } else if err == nil {
  343. err = p.validateLabel(label)
  344. }
  345. }
  346. if toASCII {
  347. for labels.reset(); !labels.done(); labels.next() {
  348. label := labels.label()
  349. if !ascii(label) {
  350. a, err2 := encode(acePrefix, label)
  351. if err == nil {
  352. err = err2
  353. }
  354. label = a
  355. labels.set(a)
  356. }
  357. n := len(label)
  358. if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
  359. err = &labelError{label, "A4"}
  360. }
  361. }
  362. }
  363. s = labels.result()
  364. if toASCII && p.verifyDNSLength && err == nil {
  365. // Compute the length of the domain name minus the root label and its dot.
  366. n := len(s)
  367. if n > 0 && s[n-1] == '.' {
  368. n--
  369. }
  370. if len(s) < 1 || n > 253 {
  371. err = &labelError{s, "A4"}
  372. }
  373. }
  374. return s, err
  375. }
  376. func normalize(p *Profile, s string) (string, error) {
  377. return norm.NFC.String(s), nil
  378. }
  379. func validateRegistration(p *Profile, s string) (string, error) {
  380. if !norm.NFC.IsNormalString(s) {
  381. return s, &labelError{s, "V1"}
  382. }
  383. for i := 0; i < len(s); {
  384. v, sz := trie.lookupString(s[i:])
  385. // Copy bytes not copied so far.
  386. switch p.simplify(info(v).category()) {
  387. // TODO: handle the NV8 defined in the Unicode idna data set to allow
  388. // for strict conformance to IDNA2008.
  389. case valid, deviation:
  390. case disallowed, mapped, unknown, ignored:
  391. r, _ := utf8.DecodeRuneInString(s[i:])
  392. return s, runeError(r)
  393. }
  394. i += sz
  395. }
  396. return s, nil
  397. }
  398. func validateAndMap(p *Profile, s string) (string, error) {
  399. var (
  400. err error
  401. b []byte
  402. k int
  403. )
  404. for i := 0; i < len(s); {
  405. v, sz := trie.lookupString(s[i:])
  406. start := i
  407. i += sz
  408. // Copy bytes not copied so far.
  409. switch p.simplify(info(v).category()) {
  410. case valid:
  411. continue
  412. case disallowed:
  413. if err == nil {
  414. r, _ := utf8.DecodeRuneInString(s[start:])
  415. err = runeError(r)
  416. }
  417. continue
  418. case mapped, deviation:
  419. b = append(b, s[k:start]...)
  420. b = info(v).appendMapping(b, s[start:i])
  421. case ignored:
  422. b = append(b, s[k:start]...)
  423. // drop the rune
  424. case unknown:
  425. b = append(b, s[k:start]...)
  426. b = append(b, "\ufffd"...)
  427. }
  428. k = i
  429. }
  430. if k == 0 {
  431. // No changes so far.
  432. s = norm.NFC.String(s)
  433. } else {
  434. b = append(b, s[k:]...)
  435. if norm.NFC.QuickSpan(b) != len(b) {
  436. b = norm.NFC.Bytes(b)
  437. }
  438. // TODO: the punycode converters require strings as input.
  439. s = string(b)
  440. }
  441. return s, err
  442. }
  443. // A labelIter allows iterating over domain name labels.
  444. type labelIter struct {
  445. orig string
  446. slice []string
  447. curStart int
  448. curEnd int
  449. i int
  450. }
  451. func (l *labelIter) reset() {
  452. l.curStart = 0
  453. l.curEnd = 0
  454. l.i = 0
  455. }
  456. func (l *labelIter) done() bool {
  457. return l.curStart >= len(l.orig)
  458. }
  459. func (l *labelIter) result() string {
  460. if l.slice != nil {
  461. return strings.Join(l.slice, ".")
  462. }
  463. return l.orig
  464. }
  465. func (l *labelIter) label() string {
  466. if l.slice != nil {
  467. return l.slice[l.i]
  468. }
  469. p := strings.IndexByte(l.orig[l.curStart:], '.')
  470. l.curEnd = l.curStart + p
  471. if p == -1 {
  472. l.curEnd = len(l.orig)
  473. }
  474. return l.orig[l.curStart:l.curEnd]
  475. }
  476. // next sets the value to the next label. It skips the last label if it is empty.
  477. func (l *labelIter) next() {
  478. l.i++
  479. if l.slice != nil {
  480. if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
  481. l.curStart = len(l.orig)
  482. }
  483. } else {
  484. l.curStart = l.curEnd + 1
  485. if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
  486. l.curStart = len(l.orig)
  487. }
  488. }
  489. }
  490. func (l *labelIter) set(s string) {
  491. if l.slice == nil {
  492. l.slice = strings.Split(l.orig, ".")
  493. }
  494. l.slice[l.i] = s
  495. }
  496. // acePrefix is the ASCII Compatible Encoding prefix.
  497. const acePrefix = "xn--"
  498. func (p *Profile) simplify(cat category) category {
  499. switch cat {
  500. case disallowedSTD3Mapped:
  501. if p.useSTD3Rules {
  502. cat = disallowed
  503. } else {
  504. cat = mapped
  505. }
  506. case disallowedSTD3Valid:
  507. if p.useSTD3Rules {
  508. cat = disallowed
  509. } else {
  510. cat = valid
  511. }
  512. case deviation:
  513. if !p.transitional {
  514. cat = valid
  515. }
  516. case validNV8, validXV8:
  517. // TODO: handle V2008
  518. cat = valid
  519. }
  520. return cat
  521. }
  522. func validateFromPunycode(p *Profile, s string) error {
  523. if !norm.NFC.IsNormalString(s) {
  524. return &labelError{s, "V1"}
  525. }
  526. for i := 0; i < len(s); {
  527. v, sz := trie.lookupString(s[i:])
  528. if c := p.simplify(info(v).category()); c != valid && c != deviation {
  529. return &labelError{s, "V6"}
  530. }
  531. i += sz
  532. }
  533. return nil
  534. }
  535. const (
  536. zwnj = "\u200c"
  537. zwj = "\u200d"
  538. )
  539. type joinState int8
  540. const (
  541. stateStart joinState = iota
  542. stateVirama
  543. stateBefore
  544. stateBeforeVirama
  545. stateAfter
  546. stateFAIL
  547. )
  548. var joinStates = [][numJoinTypes]joinState{
  549. stateStart: {
  550. joiningL: stateBefore,
  551. joiningD: stateBefore,
  552. joinZWNJ: stateFAIL,
  553. joinZWJ: stateFAIL,
  554. joinVirama: stateVirama,
  555. },
  556. stateVirama: {
  557. joiningL: stateBefore,
  558. joiningD: stateBefore,
  559. },
  560. stateBefore: {
  561. joiningL: stateBefore,
  562. joiningD: stateBefore,
  563. joiningT: stateBefore,
  564. joinZWNJ: stateAfter,
  565. joinZWJ: stateFAIL,
  566. joinVirama: stateBeforeVirama,
  567. },
  568. stateBeforeVirama: {
  569. joiningL: stateBefore,
  570. joiningD: stateBefore,
  571. joiningT: stateBefore,
  572. },
  573. stateAfter: {
  574. joiningL: stateFAIL,
  575. joiningD: stateBefore,
  576. joiningT: stateAfter,
  577. joiningR: stateStart,
  578. joinZWNJ: stateFAIL,
  579. joinZWJ: stateFAIL,
  580. joinVirama: stateAfter, // no-op as we can't accept joiners here
  581. },
  582. stateFAIL: {
  583. 0: stateFAIL,
  584. joiningL: stateFAIL,
  585. joiningD: stateFAIL,
  586. joiningT: stateFAIL,
  587. joiningR: stateFAIL,
  588. joinZWNJ: stateFAIL,
  589. joinZWJ: stateFAIL,
  590. joinVirama: stateFAIL,
  591. },
  592. }
  593. // validateLabel validates the criteria from Section 4.1. Item 1, 4, and 6 are
  594. // already implicitly satisfied by the overall implementation.
  595. func (p *Profile) validateLabel(s string) error {
  596. if s == "" {
  597. if p.verifyDNSLength {
  598. return &labelError{s, "A4"}
  599. }
  600. return nil
  601. }
  602. if p.bidirule != nil && !p.bidirule(s) {
  603. return &labelError{s, "B"}
  604. }
  605. if p.checkHyphens {
  606. if len(s) > 4 && s[2] == '-' && s[3] == '-' {
  607. return &labelError{s, "V2"}
  608. }
  609. if s[0] == '-' || s[len(s)-1] == '-' {
  610. return &labelError{s, "V3"}
  611. }
  612. }
  613. if !p.checkJoiners {
  614. return nil
  615. }
  616. trie := p.trie // p.checkJoiners is only set if trie is set.
  617. // TODO: merge the use of this in the trie.
  618. v, sz := trie.lookupString(s)
  619. x := info(v)
  620. if x.isModifier() {
  621. return &labelError{s, "V5"}
  622. }
  623. // Quickly return in the absence of zero-width (non) joiners.
  624. if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
  625. return nil
  626. }
  627. st := stateStart
  628. for i := 0; ; {
  629. jt := x.joinType()
  630. if s[i:i+sz] == zwj {
  631. jt = joinZWJ
  632. } else if s[i:i+sz] == zwnj {
  633. jt = joinZWNJ
  634. }
  635. st = joinStates[st][jt]
  636. if x.isViramaModifier() {
  637. st = joinStates[st][joinVirama]
  638. }
  639. if i += sz; i == len(s) {
  640. break
  641. }
  642. v, sz = trie.lookupString(s[i:])
  643. x = info(v)
  644. }
  645. if st == stateFAIL || st == stateAfter {
  646. return &labelError{s, "C"}
  647. }
  648. return nil
  649. }
  650. func ascii(s string) bool {
  651. for i := 0; i < len(s); i++ {
  652. if s[i] >= utf8.RuneSelf {
  653. return false
  654. }
  655. }
  656. return true
  657. }