idna9.0.0.go 19 KB

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