Browse Source

upgrade x/text

tags/v2.7.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
6f56121662
39 changed files with 334 additions and 125 deletions
  1. 1
    1
      go.mod
  2. 2
    0
      go.sum
  3. 1
    0
      vendor/golang.org/x/text/cases/icu.go
  4. 1
    0
      vendor/golang.org/x/text/cases/tables10.0.0.go
  5. 1
    0
      vendor/golang.org/x/text/cases/tables11.0.0.go
  6. 1
    0
      vendor/golang.org/x/text/cases/tables12.0.0.go
  7. 1
    0
      vendor/golang.org/x/text/cases/tables13.0.0.go
  8. 1
    0
      vendor/golang.org/x/text/cases/tables9.0.0.go
  9. 43
    47
      vendor/golang.org/x/text/internal/language/language.go
  10. 20
    17
      vendor/golang.org/x/text/internal/language/parse.go
  11. 1
    0
      vendor/golang.org/x/text/language/go1_1.go
  12. 1
    0
      vendor/golang.org/x/text/language/go1_2.go
  13. 4
    0
      vendor/golang.org/x/text/language/language.go
  14. 4
    4
      vendor/golang.org/x/text/language/tables.go
  15. 1
    0
      vendor/golang.org/x/text/secure/bidirule/bidirule10.0.0.go
  16. 1
    0
      vendor/golang.org/x/text/secure/bidirule/bidirule9.0.0.go
  17. 1
    0
      vendor/golang.org/x/text/secure/precis/tables10.0.0.go
  18. 1
    0
      vendor/golang.org/x/text/secure/precis/tables11.0.0.go
  19. 1
    0
      vendor/golang.org/x/text/secure/precis/tables12.0.0.go
  20. 1
    0
      vendor/golang.org/x/text/secure/precis/tables13.0.0.go
  21. 1
    0
      vendor/golang.org/x/text/secure/precis/tables9.0.0.go
  22. 191
    30
      vendor/golang.org/x/text/unicode/bidi/bidi.go
  23. 38
    25
      vendor/golang.org/x/text/unicode/bidi/core.go
  24. 1
    0
      vendor/golang.org/x/text/unicode/bidi/tables10.0.0.go
  25. 1
    0
      vendor/golang.org/x/text/unicode/bidi/tables11.0.0.go
  26. 1
    0
      vendor/golang.org/x/text/unicode/bidi/tables12.0.0.go
  27. 1
    0
      vendor/golang.org/x/text/unicode/bidi/tables13.0.0.go
  28. 1
    0
      vendor/golang.org/x/text/unicode/bidi/tables9.0.0.go
  29. 1
    0
      vendor/golang.org/x/text/unicode/norm/tables10.0.0.go
  30. 1
    0
      vendor/golang.org/x/text/unicode/norm/tables11.0.0.go
  31. 1
    0
      vendor/golang.org/x/text/unicode/norm/tables12.0.0.go
  32. 1
    0
      vendor/golang.org/x/text/unicode/norm/tables13.0.0.go
  33. 1
    0
      vendor/golang.org/x/text/unicode/norm/tables9.0.0.go
  34. 1
    0
      vendor/golang.org/x/text/width/tables10.0.0.go
  35. 1
    0
      vendor/golang.org/x/text/width/tables11.0.0.go
  36. 1
    0
      vendor/golang.org/x/text/width/tables12.0.0.go
  37. 1
    0
      vendor/golang.org/x/text/width/tables13.0.0.go
  38. 1
    0
      vendor/golang.org/x/text/width/tables9.0.0.go
  39. 1
    1
      vendor/modules.txt

+ 1
- 1
go.mod View File

@@ -20,7 +20,7 @@ require (
20 20
 	github.com/tidwall/rtree v0.0.0-20201027154624-32188eeb08a8 // indirect
21 21
 	github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208
22 22
 	golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc
23
-	golang.org/x/text v0.3.4
23
+	golang.org/x/text v0.3.6
24 24
 	gopkg.in/yaml.v2 v2.3.0
25 25
 )
26 26
 

+ 2
- 0
go.sum View File

@@ -130,6 +130,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
130 130
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
131 131
 golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
132 132
 golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
133
+golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
134
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
133 135
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
134 136
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
135 137
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

+ 1
- 0
vendor/golang.org/x/text/cases/icu.go View File

@@ -2,6 +2,7 @@
2 2
 // Use of this source code is governed by a BSD-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
+//go:build icu
5 6
 // +build icu
6 7
 
7 8
 package cases

+ 1
- 0
vendor/golang.org/x/text/cases/tables10.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.10 && !go1.13
3 4
 // +build go1.10,!go1.13
4 5
 
5 6
 package cases

+ 1
- 0
vendor/golang.org/x/text/cases/tables11.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.13 && !go1.14
3 4
 // +build go1.13,!go1.14
4 5
 
5 6
 package cases

+ 1
- 0
vendor/golang.org/x/text/cases/tables12.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.14 && !go1.16
3 4
 // +build go1.14,!go1.16
4 5
 
5 6
 package cases

+ 1
- 0
vendor/golang.org/x/text/cases/tables13.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.16
3 4
 // +build go1.16
4 5
 
5 6
 package cases

+ 1
- 0
vendor/golang.org/x/text/cases/tables9.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build !go1.10
3 4
 // +build !go1.10
4 5
 
5 6
 package cases

+ 43
- 47
vendor/golang.org/x/text/internal/language/language.go View File

@@ -303,9 +303,17 @@ func (t Tag) Extensions() []string {
303 303
 // are of the allowed values defined for the Unicode locale extension ('u') in
304 304
 // https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
305 305
 // TypeForKey will traverse the inheritance chain to get the correct value.
306
+//
307
+// If there are multiple types associated with a key, only the first will be
308
+// returned. If there is no type associated with a key, it returns the empty
309
+// string.
306 310
 func (t Tag) TypeForKey(key string) string {
307
-	if start, end, _ := t.findTypeForKey(key); end != start {
308
-		return t.str[start:end]
311
+	if _, start, end, _ := t.findTypeForKey(key); end != start {
312
+		s := t.str[start:end]
313
+		if p := strings.IndexByte(s, '-'); p >= 0 {
314
+			s = s[:p]
315
+		}
316
+		return s
309 317
 	}
310 318
 	return ""
311 319
 }
@@ -329,13 +337,13 @@ func (t Tag) SetTypeForKey(key, value string) (Tag, error) {
329 337
 
330 338
 	// Remove the setting if value is "".
331 339
 	if value == "" {
332
-		start, end, _ := t.findTypeForKey(key)
333
-		if start != end {
334
-			// Remove key tag and leading '-'.
335
-			start -= 4
336
-
340
+		start, sep, end, _ := t.findTypeForKey(key)
341
+		if start != sep {
337 342
 			// Remove a possible empty extension.
338
-			if (end == len(t.str) || t.str[end+2] == '-') && t.str[start-2] == '-' {
343
+			switch {
344
+			case t.str[start-2] != '-': // has previous elements.
345
+			case end == len(t.str), // end of string
346
+				end+2 < len(t.str) && t.str[end+2] == '-': // end of extension
339 347
 				start -= 2
340 348
 			}
341 349
 			if start == int(t.pVariant) && end == len(t.str) {
@@ -381,14 +389,14 @@ func (t Tag) SetTypeForKey(key, value string) (Tag, error) {
381 389
 		t.str = string(buf[:uStart+len(b)])
382 390
 	} else {
383 391
 		s := t.str
384
-		start, end, hasExt := t.findTypeForKey(key)
385
-		if start == end {
392
+		start, sep, end, hasExt := t.findTypeForKey(key)
393
+		if start == sep {
386 394
 			if hasExt {
387 395
 				b = b[2:]
388 396
 			}
389
-			t.str = fmt.Sprintf("%s-%s%s", s[:start], b, s[end:])
397
+			t.str = fmt.Sprintf("%s-%s%s", s[:sep], b, s[end:])
390 398
 		} else {
391
-			t.str = fmt.Sprintf("%s%s%s", s[:start], value, s[end:])
399
+			t.str = fmt.Sprintf("%s-%s%s", s[:start+3], value, s[end:])
392 400
 		}
393 401
 	}
394 402
 	return t, nil
@@ -399,10 +407,10 @@ func (t Tag) SetTypeForKey(key, value string) (Tag, error) {
399 407
 // wasn't found. The hasExt return value reports whether an -u extension was present.
400 408
 // Note: the extensions are typically very small and are likely to contain
401 409
 // only one key-type pair.
402
-func (t Tag) findTypeForKey(key string) (start, end int, hasExt bool) {
410
+func (t Tag) findTypeForKey(key string) (start, sep, end int, hasExt bool) {
403 411
 	p := int(t.pExt)
404 412
 	if len(key) != 2 || p == len(t.str) || p == 0 {
405
-		return p, p, false
413
+		return p, p, p, false
406 414
 	}
407 415
 	s := t.str
408 416
 
@@ -410,10 +418,10 @@ func (t Tag) findTypeForKey(key string) (start, end int, hasExt bool) {
410 418
 	for p++; s[p] != 'u'; p++ {
411 419
 		if s[p] > 'u' {
412 420
 			p--
413
-			return p, p, false
421
+			return p, p, p, false
414 422
 		}
415 423
 		if p = nextExtension(s, p); p == len(s) {
416
-			return len(s), len(s), false
424
+			return len(s), len(s), len(s), false
417 425
 		}
418 426
 	}
419 427
 	// Proceed to the hyphen following the extension name.
@@ -424,40 +432,28 @@ func (t Tag) findTypeForKey(key string) (start, end int, hasExt bool) {
424 432
 
425 433
 	// Iterate over keys until we get the end of a section.
426 434
 	for {
427
-		// p points to the hyphen preceding the current token.
428
-		if p3 := p + 3; s[p3] == '-' {
429
-			// Found a key.
430
-			// Check whether we just processed the key that was requested.
431
-			if curKey == key {
432
-				return start, p, true
433
-			}
434
-			// Set to the next key and continue scanning type tokens.
435
-			curKey = s[p+1 : p3]
436
-			if curKey > key {
437
-				return p, p, true
438
-			}
439
-			// Start of the type token sequence.
440
-			start = p + 4
441
-			// A type is at least 3 characters long.
442
-			p += 7 // 4 + 3
443
-		} else {
444
-			// Attribute or type, which is at least 3 characters long.
445
-			p += 4
446
-		}
447
-		// p points past the third character of a type or attribute.
448
-		max := p + 5 // maximum length of token plus hyphen.
449
-		if len(s) < max {
450
-			max = len(s)
435
+		end = p
436
+		for p++; p < len(s) && s[p] != '-'; p++ {
451 437
 		}
452
-		for ; p < max && s[p] != '-'; p++ {
438
+		n := p - end - 1
439
+		if n <= 2 && curKey == key {
440
+			if sep < end {
441
+				sep++
442
+			}
443
+			return start, sep, end, true
453 444
 		}
454
-		// Bail if we have exhausted all tokens or if the next token starts
455
-		// a new extension.
456
-		if p == len(s) || s[p+2] == '-' {
457
-			if curKey == key {
458
-				return start, p, true
445
+		switch n {
446
+		case 0, // invalid string
447
+			1: // next extension
448
+			return end, end, end, true
449
+		case 2:
450
+			// next key
451
+			curKey = s[end+1 : p]
452
+			if curKey > key {
453
+				return end, end, end, true
459 454
 			}
460
-			return p, p, true
455
+			start = end
456
+			sep = p
461 457
 		}
462 458
 	}
463 459
 }

+ 20
- 17
vendor/golang.org/x/text/internal/language/parse.go View File

@@ -133,14 +133,15 @@ func (s *scanner) resizeRange(oldStart, oldEnd, newSize int) {
133 133
 	s.start = oldStart
134 134
 	if end := oldStart + newSize; end != oldEnd {
135 135
 		diff := end - oldEnd
136
-		if end < cap(s.b) {
137
-			b := make([]byte, len(s.b)+diff)
136
+		var b []byte
137
+		if n := len(s.b) + diff; n > cap(s.b) {
138
+			b = make([]byte, n)
138 139
 			copy(b, s.b[:oldStart])
139
-			copy(b[end:], s.b[oldEnd:])
140
-			s.b = b
141 140
 		} else {
142
-			s.b = append(s.b[end:], s.b[oldEnd:]...)
141
+			b = s.b[:n]
143 142
 		}
143
+		copy(b[end:], s.b[oldEnd:])
144
+		s.b = b
144 145
 		s.next = end + (s.next - s.end)
145 146
 		s.end = end
146 147
 	}
@@ -482,7 +483,7 @@ func parseExtensions(scan *scanner) int {
482 483
 func parseExtension(scan *scanner) int {
483 484
 	start, end := scan.start, scan.end
484 485
 	switch scan.token[0] {
485
-	case 'u':
486
+	case 'u': // https://www.ietf.org/rfc/rfc6067.txt
486 487
 		attrStart := end
487 488
 		scan.scan()
488 489
 		for last := []byte{}; len(scan.token) > 2; scan.scan() {
@@ -502,27 +503,29 @@ func parseExtension(scan *scanner) int {
502 503
 			last = scan.token
503 504
 			end = scan.end
504 505
 		}
506
+		// Scan key-type sequences. A key is of length 2 and may be followed
507
+		// by 0 or more "type" subtags from 3 to the maximum of 8 letters.
505 508
 		var last, key []byte
506 509
 		for attrEnd := end; len(scan.token) == 2; last = key {
507 510
 			key = scan.token
508
-			keyEnd := scan.end
509
-			end = scan.acceptMinSize(3)
511
+			end = scan.end
512
+			for scan.scan(); end < scan.end && len(scan.token) > 2; scan.scan() {
513
+				end = scan.end
514
+			}
510 515
 			// TODO: check key value validity
511
-			if keyEnd == end || bytes.Compare(key, last) != 1 {
516
+			if bytes.Compare(key, last) != 1 || scan.err != nil {
512 517
 				// We have an invalid key or the keys are not sorted.
513 518
 				// Start scanning keys from scratch and reorder.
514 519
 				p := attrEnd + 1
515 520
 				scan.next = p
516 521
 				keys := [][]byte{}
517 522
 				for scan.scan(); len(scan.token) == 2; {
518
-					keyStart, keyEnd := scan.start, scan.end
519
-					end = scan.acceptMinSize(3)
520
-					if keyEnd != end {
521
-						keys = append(keys, scan.b[keyStart:end])
522
-					} else {
523
-						scan.setError(ErrSyntax)
524
-						end = keyStart
523
+					keyStart := scan.start
524
+					end = scan.end
525
+					for scan.scan(); end < scan.end && len(scan.token) > 2; scan.scan() {
526
+						end = scan.end
525 527
 					}
528
+					keys = append(keys, scan.b[keyStart:end])
526 529
 				}
527 530
 				sort.Stable(bytesSort{keys, 2})
528 531
 				if n := len(keys); n > 0 {
@@ -546,7 +549,7 @@ func parseExtension(scan *scanner) int {
546 549
 				break
547 550
 			}
548 551
 		}
549
-	case 't':
552
+	case 't': // https://www.ietf.org/rfc/rfc6497.txt
550 553
 		scan.scan()
551 554
 		if n := len(scan.token); n >= 2 && n <= 3 && isAlpha(scan.token[1]) {
552 555
 			_, end = parseTag(scan)

+ 1
- 0
vendor/golang.org/x/text/language/go1_1.go View File

@@ -2,6 +2,7 @@
2 2
 // Use of this source code is governed by a BSD-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
+//go:build !go1.2
5 6
 // +build !go1.2
6 7
 
7 8
 package language

+ 1
- 0
vendor/golang.org/x/text/language/go1_2.go View File

@@ -2,6 +2,7 @@
2 2
 // Use of this source code is governed by a BSD-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
+//go:build go1.2
5 6
 // +build go1.2
6 7
 
7 8
 package language

+ 4
- 0
vendor/golang.org/x/text/language/language.go View File

@@ -412,6 +412,10 @@ func (t Tag) Extensions() []Extension {
412 412
 // are of the allowed values defined for the Unicode locale extension ('u') in
413 413
 // https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
414 414
 // TypeForKey will traverse the inheritance chain to get the correct value.
415
+//
416
+// If there are multiple types associated with a key, only the first will be
417
+// returned. If there is no type associated with a key, it returns the empty
418
+// string.
415 419
 func (t Tag) TypeForKey(key string) string {
416 420
 	if !compact.Tag(t).MayHaveExtensions() {
417 421
 		if key != "rg" && key != "va" {

+ 4
- 4
vendor/golang.org/x/text/language/tables.go View File

@@ -47,7 +47,7 @@ const (
47 47
 	_Zzzz = 251
48 48
 )
49 49
 
50
-var regionToGroups = []uint8{ // 357 elements
50
+var regionToGroups = []uint8{ // 358 elements
51 51
 	// Entry 0 - 3F
52 52
 	0x00, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x04,
53 53
 	0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x00,
@@ -98,8 +98,8 @@ var regionToGroups = []uint8{ // 357 elements
98 98
 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 99
 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 100
 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101
-	0x00, 0x00, 0x00, 0x00, 0x00,
102
-} // Size: 381 bytes
101
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102
+} // Size: 382 bytes
103 103
 
104 104
 var paradigmLocales = [][3]uint16{ // 3 elements
105 105
 	0: [3]uint16{0x139, 0x0, 0x7b},
@@ -295,4 +295,4 @@ var matchRegion = []regionIntelligibility{ // 15 elements
295 295
 	14: {lang: 0x529, script: 0x3c, group: 0x80, distance: 0x5},
296 296
 } // Size: 114 bytes
297 297
 
298
-// Total table size 1471 bytes (1KiB); checksum: 4CB1CD46
298
+// Total table size 1472 bytes (1KiB); checksum: F86C669

+ 1
- 0
vendor/golang.org/x/text/secure/bidirule/bidirule10.0.0.go View File

@@ -2,6 +2,7 @@
2 2
 // Use of this source code is governed by a BSD-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
+//go:build go1.10
5 6
 // +build go1.10
6 7
 
7 8
 package bidirule

+ 1
- 0
vendor/golang.org/x/text/secure/bidirule/bidirule9.0.0.go View File

@@ -2,6 +2,7 @@
2 2
 // Use of this source code is governed by a BSD-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
+//go:build !go1.10
5 6
 // +build !go1.10
6 7
 
7 8
 package bidirule

+ 1
- 0
vendor/golang.org/x/text/secure/precis/tables10.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.10 && !go1.13
3 4
 // +build go1.10,!go1.13
4 5
 
5 6
 package precis

+ 1
- 0
vendor/golang.org/x/text/secure/precis/tables11.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.13 && !go1.14
3 4
 // +build go1.13,!go1.14
4 5
 
5 6
 package precis

+ 1
- 0
vendor/golang.org/x/text/secure/precis/tables12.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.14 && !go1.16
3 4
 // +build go1.14,!go1.16
4 5
 
5 6
 package precis

+ 1
- 0
vendor/golang.org/x/text/secure/precis/tables13.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.16
3 4
 // +build go1.16
4 5
 
5 6
 package precis

+ 1
- 0
vendor/golang.org/x/text/secure/precis/tables9.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build !go1.10
3 4
 // +build !go1.10
4 5
 
5 6
 package precis

+ 191
- 30
vendor/golang.org/x/text/unicode/bidi/bidi.go View File

@@ -12,15 +12,14 @@
12 12
 // and without notice.
13 13
 package bidi // import "golang.org/x/text/unicode/bidi"
14 14
 
15
-// TODO:
16
-// The following functionality would not be hard to implement, but hinges on
17
-// the definition of a Segmenter interface. For now this is up to the user.
18
-// - Iterate over paragraphs
19
-// - Segmenter to iterate over runs directly from a given text.
20
-// Also:
15
+// TODO
21 16
 // - Transformer for reordering?
22 17
 // - Transformer (validator, really) for Bidi Rule.
23 18
 
19
+import (
20
+	"bytes"
21
+)
22
+
24 23
 // This API tries to avoid dealing with embedding levels for now. Under the hood
25 24
 // these will be computed, but the question is to which extent the user should
26 25
 // know they exist. We should at some point allow the user to specify an
@@ -49,7 +48,9 @@ const (
49 48
 	Neutral
50 49
 )
51 50
 
52
-type options struct{}
51
+type options struct {
52
+	defaultDirection Direction
53
+}
53 54
 
54 55
 // An Option is an option for Bidi processing.
55 56
 type Option func(*options)
@@ -66,12 +67,62 @@ type Option func(*options)
66 67
 // DefaultDirection sets the default direction for a Paragraph. The direction is
67 68
 // overridden if the text contains directional characters.
68 69
 func DefaultDirection(d Direction) Option {
69
-	panic("unimplemented")
70
+	return func(opts *options) {
71
+		opts.defaultDirection = d
72
+	}
70 73
 }
71 74
 
72 75
 // A Paragraph holds a single Paragraph for Bidi processing.
73 76
 type Paragraph struct {
74
-	// buffers
77
+	p          []byte
78
+	o          Ordering
79
+	opts       []Option
80
+	types      []Class
81
+	pairTypes  []bracketType
82
+	pairValues []rune
83
+	runes      []rune
84
+	options    options
85
+}
86
+
87
+// Initialize the p.pairTypes, p.pairValues and p.types from the input previously
88
+// set by p.SetBytes() or p.SetString(). Also limit the input up to (and including) a paragraph
89
+// separator (bidi class B).
90
+//
91
+// The function p.Order() needs these values to be set, so this preparation could be postponed.
92
+// But since the SetBytes and SetStrings functions return the length of the input up to the paragraph
93
+// separator, the whole input needs to be processed anyway and should not be done twice.
94
+//
95
+// The function has the same return values as SetBytes() / SetString()
96
+func (p *Paragraph) prepareInput() (n int, err error) {
97
+	p.runes = bytes.Runes(p.p)
98
+	bytecount := 0
99
+	// clear slices from previous SetString or SetBytes
100
+	p.pairTypes = nil
101
+	p.pairValues = nil
102
+	p.types = nil
103
+
104
+	for _, r := range p.runes {
105
+		props, i := LookupRune(r)
106
+		bytecount += i
107
+		cls := props.Class()
108
+		if cls == B {
109
+			return bytecount, nil
110
+		}
111
+		p.types = append(p.types, cls)
112
+		if props.IsOpeningBracket() {
113
+			p.pairTypes = append(p.pairTypes, bpOpen)
114
+			p.pairValues = append(p.pairValues, r)
115
+		} else if props.IsBracket() {
116
+			// this must be a closing bracket,
117
+			// since IsOpeningBracket is not true
118
+			p.pairTypes = append(p.pairTypes, bpClose)
119
+			p.pairValues = append(p.pairValues, r)
120
+		} else {
121
+			p.pairTypes = append(p.pairTypes, bpNone)
122
+			p.pairValues = append(p.pairValues, 0)
123
+		}
124
+	}
125
+	return bytecount, nil
75 126
 }
76 127
 
77 128
 // SetBytes configures p for the given paragraph text. It replaces text
@@ -80,70 +131,150 @@ type Paragraph struct {
80 131
 // consumed from b including this separator. Error may be non-nil if options are
81 132
 // given.
82 133
 func (p *Paragraph) SetBytes(b []byte, opts ...Option) (n int, err error) {
83
-	panic("unimplemented")
134
+	p.p = b
135
+	p.opts = opts
136
+	return p.prepareInput()
84 137
 }
85 138
 
86
-// SetString configures p for the given paragraph text. It replaces text
87
-// previously set by SetBytes or SetString. If b contains a paragraph separator
139
+// SetString configures s for the given paragraph text. It replaces text
140
+// previously set by SetBytes or SetString. If s contains a paragraph separator
88 141
 // it will only process the first paragraph and report the number of bytes
89
-// consumed from b including this separator. Error may be non-nil if options are
142
+// consumed from s including this separator. Error may be non-nil if options are
90 143
 // given.
91 144
 func (p *Paragraph) SetString(s string, opts ...Option) (n int, err error) {
92
-	panic("unimplemented")
145
+	p.p = []byte(s)
146
+	p.opts = opts
147
+	return p.prepareInput()
93 148
 }
94 149
 
95 150
 // IsLeftToRight reports whether the principle direction of rendering for this
96 151
 // paragraphs is left-to-right. If this returns false, the principle direction
97 152
 // of rendering is right-to-left.
98 153
 func (p *Paragraph) IsLeftToRight() bool {
99
-	panic("unimplemented")
154
+	return p.Direction() == LeftToRight
100 155
 }
101 156
 
102 157
 // Direction returns the direction of the text of this paragraph.
103 158
 //
104 159
 // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral.
105 160
 func (p *Paragraph) Direction() Direction {
106
-	panic("unimplemented")
161
+	return p.o.Direction()
107 162
 }
108 163
 
164
+// TODO: what happens if the position is > len(input)? This should return an error.
165
+
109 166
 // RunAt reports the Run at the given position of the input text.
110 167
 //
111 168
 // This method can be used for computing line breaks on paragraphs.
112 169
 func (p *Paragraph) RunAt(pos int) Run {
113
-	panic("unimplemented")
170
+	c := 0
171
+	runNumber := 0
172
+	for i, r := range p.o.runes {
173
+		c += len(r)
174
+		if pos < c {
175
+			runNumber = i
176
+		}
177
+	}
178
+	return p.o.Run(runNumber)
179
+}
180
+
181
+func calculateOrdering(levels []level, runes []rune) Ordering {
182
+	var curDir Direction
183
+
184
+	prevDir := Neutral
185
+	prevI := 0
186
+
187
+	o := Ordering{}
188
+	// lvl = 0,2,4,...: left to right
189
+	// lvl = 1,3,5,...: right to left
190
+	for i, lvl := range levels {
191
+		if lvl%2 == 0 {
192
+			curDir = LeftToRight
193
+		} else {
194
+			curDir = RightToLeft
195
+		}
196
+		if curDir != prevDir {
197
+			if i > 0 {
198
+				o.runes = append(o.runes, runes[prevI:i])
199
+				o.directions = append(o.directions, prevDir)
200
+				o.startpos = append(o.startpos, prevI)
201
+			}
202
+			prevI = i
203
+			prevDir = curDir
204
+		}
205
+	}
206
+	o.runes = append(o.runes, runes[prevI:])
207
+	o.directions = append(o.directions, prevDir)
208
+	o.startpos = append(o.startpos, prevI)
209
+	return o
114 210
 }
115 211
 
116 212
 // Order computes the visual ordering of all the runs in a Paragraph.
117 213
 func (p *Paragraph) Order() (Ordering, error) {
118
-	panic("unimplemented")
214
+	if len(p.types) == 0 {
215
+		return Ordering{}, nil
216
+	}
217
+
218
+	for _, fn := range p.opts {
219
+		fn(&p.options)
220
+	}
221
+	lvl := level(-1)
222
+	if p.options.defaultDirection == RightToLeft {
223
+		lvl = 1
224
+	}
225
+	para, err := newParagraph(p.types, p.pairTypes, p.pairValues, lvl)
226
+	if err != nil {
227
+		return Ordering{}, err
228
+	}
229
+
230
+	levels := para.getLevels([]int{len(p.types)})
231
+
232
+	p.o = calculateOrdering(levels, p.runes)
233
+	return p.o, nil
119 234
 }
120 235
 
121 236
 // Line computes the visual ordering of runs for a single line starting and
122 237
 // ending at the given positions in the original text.
123 238
 func (p *Paragraph) Line(start, end int) (Ordering, error) {
124
-	panic("unimplemented")
239
+	lineTypes := p.types[start:end]
240
+	para, err := newParagraph(lineTypes, p.pairTypes[start:end], p.pairValues[start:end], -1)
241
+	if err != nil {
242
+		return Ordering{}, err
243
+	}
244
+	levels := para.getLevels([]int{len(lineTypes)})
245
+	o := calculateOrdering(levels, p.runes[start:end])
246
+	return o, nil
125 247
 }
126 248
 
127 249
 // An Ordering holds the computed visual order of runs of a Paragraph. Calling
128 250
 // SetBytes or SetString on the originating Paragraph invalidates an Ordering.
129 251
 // The methods of an Ordering should only be called by one goroutine at a time.
130
-type Ordering struct{}
252
+type Ordering struct {
253
+	runes      [][]rune
254
+	directions []Direction
255
+	startpos   []int
256
+}
131 257
 
132 258
 // Direction reports the directionality of the runs.
133 259
 //
134 260
 // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral.
135 261
 func (o *Ordering) Direction() Direction {
136
-	panic("unimplemented")
262
+	return o.directions[0]
137 263
 }
138 264
 
139 265
 // NumRuns returns the number of runs.
140 266
 func (o *Ordering) NumRuns() int {
141
-	panic("unimplemented")
267
+	return len(o.runes)
142 268
 }
143 269
 
144 270
 // Run returns the ith run within the ordering.
145 271
 func (o *Ordering) Run(i int) Run {
146
-	panic("unimplemented")
272
+	r := Run{
273
+		runes:     o.runes[i],
274
+		direction: o.directions[i],
275
+		startpos:  o.startpos[i],
276
+	}
277
+	return r
147 278
 }
148 279
 
149 280
 // TODO: perhaps with options.
@@ -155,16 +286,19 @@ func (o *Ordering) Run(i int) Run {
155 286
 
156 287
 // A Run is a continuous sequence of characters of a single direction.
157 288
 type Run struct {
289
+	runes     []rune
290
+	direction Direction
291
+	startpos  int
158 292
 }
159 293
 
160 294
 // String returns the text of the run in its original order.
161 295
 func (r *Run) String() string {
162
-	panic("unimplemented")
296
+	return string(r.runes)
163 297
 }
164 298
 
165 299
 // Bytes returns the text of the run in its original order.
166 300
 func (r *Run) Bytes() []byte {
167
-	panic("unimplemented")
301
+	return []byte(r.String())
168 302
 }
169 303
 
170 304
 // TODO: methods for
@@ -174,25 +308,52 @@ func (r *Run) Bytes() []byte {
174 308
 
175 309
 // Direction reports the direction of the run.
176 310
 func (r *Run) Direction() Direction {
177
-	panic("unimplemented")
311
+	return r.direction
178 312
 }
179 313
 
180
-// Position of the Run within the text passed to SetBytes or SetString of the
314
+// Pos returns the position of the Run within the text passed to SetBytes or SetString of the
181 315
 // originating Paragraph value.
182 316
 func (r *Run) Pos() (start, end int) {
183
-	panic("unimplemented")
317
+	return r.startpos, r.startpos + len(r.runes) - 1
184 318
 }
185 319
 
186 320
 // AppendReverse reverses the order of characters of in, appends them to out,
187 321
 // and returns the result. Modifiers will still follow the runes they modify.
188 322
 // Brackets are replaced with their counterparts.
189 323
 func AppendReverse(out, in []byte) []byte {
190
-	panic("unimplemented")
324
+	ret := make([]byte, len(in)+len(out))
325
+	copy(ret, out)
326
+	inRunes := bytes.Runes(in)
327
+
328
+	for i, r := range inRunes {
329
+		prop, _ := LookupRune(r)
330
+		if prop.IsBracket() {
331
+			inRunes[i] = prop.reverseBracket(r)
332
+		}
333
+	}
334
+
335
+	for i, j := 0, len(inRunes)-1; i < j; i, j = i+1, j-1 {
336
+		inRunes[i], inRunes[j] = inRunes[j], inRunes[i]
337
+	}
338
+	copy(ret[len(out):], string(inRunes))
339
+
340
+	return ret
191 341
 }
192 342
 
193 343
 // ReverseString reverses the order of characters in s and returns a new string.
194 344
 // Modifiers will still follow the runes they modify. Brackets are replaced with
195 345
 // their counterparts.
196 346
 func ReverseString(s string) string {
197
-	panic("unimplemented")
347
+	input := []rune(s)
348
+	li := len(input)
349
+	ret := make([]rune, li)
350
+	for i, r := range input {
351
+		prop, _ := LookupRune(r)
352
+		if prop.IsBracket() {
353
+			ret[li-i-1] = prop.reverseBracket(r)
354
+		} else {
355
+			ret[li-i-1] = r
356
+		}
357
+	}
358
+	return string(ret)
198 359
 }

+ 38
- 25
vendor/golang.org/x/text/unicode/bidi/core.go View File

@@ -4,7 +4,10 @@
4 4
 
5 5
 package bidi
6 6
 
7
-import "log"
7
+import (
8
+	"fmt"
9
+	"log"
10
+)
8 11
 
9 12
 // This implementation is a port based on the reference implementation found at:
10 13
 // https://www.unicode.org/Public/PROGRAMS/BidiReferenceJava/
@@ -97,13 +100,20 @@ type paragraph struct {
97 100
 // rune (suggested is the rune of the open bracket for opening and matching
98 101
 // close brackets, after normalization). The embedding levels are optional, but
99 102
 // may be supplied to encode embedding levels of styled text.
100
-//
101
-// TODO: return an error.
102
-func newParagraph(types []Class, pairTypes []bracketType, pairValues []rune, levels level) *paragraph {
103
-	validateTypes(types)
104
-	validatePbTypes(pairTypes)
105
-	validatePbValues(pairValues, pairTypes)
106
-	validateParagraphEmbeddingLevel(levels)
103
+func newParagraph(types []Class, pairTypes []bracketType, pairValues []rune, levels level) (*paragraph, error) {
104
+	var err error
105
+	if err = validateTypes(types); err != nil {
106
+		return nil, err
107
+	}
108
+	if err = validatePbTypes(pairTypes); err != nil {
109
+		return nil, err
110
+	}
111
+	if err = validatePbValues(pairValues, pairTypes); err != nil {
112
+		return nil, err
113
+	}
114
+	if err = validateParagraphEmbeddingLevel(levels); err != nil {
115
+		return nil, err
116
+	}
107 117
 
108 118
 	p := &paragraph{
109 119
 		initialTypes:   append([]Class(nil), types...),
@@ -115,7 +125,7 @@ func newParagraph(types []Class, pairTypes []bracketType, pairValues []rune, lev
115 125
 		resultTypes: append([]Class(nil), types...),
116 126
 	}
117 127
 	p.run()
118
-	return p
128
+	return p, nil
119 129
 }
120 130
 
121 131
 func (p *paragraph) Len() int { return len(p.initialTypes) }
@@ -1001,58 +1011,61 @@ func typeForLevel(level level) Class {
1001 1011
 	return R
1002 1012
 }
1003 1013
 
1004
-// TODO: change validation to not panic
1005
-
1006
-func validateTypes(types []Class) {
1014
+func validateTypes(types []Class) error {
1007 1015
 	if len(types) == 0 {
1008
-		log.Panic("types is null")
1016
+		return fmt.Errorf("types is null")
1009 1017
 	}
1010 1018
 	for i, t := range types[:len(types)-1] {
1011 1019
 		if t == B {
1012
-			log.Panicf("B type before end of paragraph at index: %d", i)
1020
+			return fmt.Errorf("B type before end of paragraph at index: %d", i)
1013 1021
 		}
1014 1022
 	}
1023
+	return nil
1015 1024
 }
1016 1025
 
1017
-func validateParagraphEmbeddingLevel(embeddingLevel level) {
1026
+func validateParagraphEmbeddingLevel(embeddingLevel level) error {
1018 1027
 	if embeddingLevel != implicitLevel &&
1019 1028
 		embeddingLevel != 0 &&
1020 1029
 		embeddingLevel != 1 {
1021
-		log.Panicf("illegal paragraph embedding level: %d", embeddingLevel)
1030
+		return fmt.Errorf("illegal paragraph embedding level: %d", embeddingLevel)
1022 1031
 	}
1032
+	return nil
1023 1033
 }
1024 1034
 
1025
-func validateLineBreaks(linebreaks []int, textLength int) {
1035
+func validateLineBreaks(linebreaks []int, textLength int) error {
1026 1036
 	prev := 0
1027 1037
 	for i, next := range linebreaks {
1028 1038
 		if next <= prev {
1029
-			log.Panicf("bad linebreak: %d at index: %d", next, i)
1039
+			return fmt.Errorf("bad linebreak: %d at index: %d", next, i)
1030 1040
 		}
1031 1041
 		prev = next
1032 1042
 	}
1033 1043
 	if prev != textLength {
1034
-		log.Panicf("last linebreak was %d, want %d", prev, textLength)
1044
+		return fmt.Errorf("last linebreak was %d, want %d", prev, textLength)
1035 1045
 	}
1046
+	return nil
1036 1047
 }
1037 1048
 
1038
-func validatePbTypes(pairTypes []bracketType) {
1049
+func validatePbTypes(pairTypes []bracketType) error {
1039 1050
 	if len(pairTypes) == 0 {
1040
-		log.Panic("pairTypes is null")
1051
+		return fmt.Errorf("pairTypes is null")
1041 1052
 	}
1042 1053
 	for i, pt := range pairTypes {
1043 1054
 		switch pt {
1044 1055
 		case bpNone, bpOpen, bpClose:
1045 1056
 		default:
1046
-			log.Panicf("illegal pairType value at %d: %v", i, pairTypes[i])
1057
+			return fmt.Errorf("illegal pairType value at %d: %v", i, pairTypes[i])
1047 1058
 		}
1048 1059
 	}
1060
+	return nil
1049 1061
 }
1050 1062
 
1051
-func validatePbValues(pairValues []rune, pairTypes []bracketType) {
1063
+func validatePbValues(pairValues []rune, pairTypes []bracketType) error {
1052 1064
 	if pairValues == nil {
1053
-		log.Panic("pairValues is null")
1065
+		return fmt.Errorf("pairValues is null")
1054 1066
 	}
1055 1067
 	if len(pairTypes) != len(pairValues) {
1056
-		log.Panic("pairTypes is different length from pairValues")
1068
+		return fmt.Errorf("pairTypes is different length from pairValues")
1057 1069
 	}
1070
+	return nil
1058 1071
 }

+ 1
- 0
vendor/golang.org/x/text/unicode/bidi/tables10.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.10 && !go1.13
3 4
 // +build go1.10,!go1.13
4 5
 
5 6
 package bidi

+ 1
- 0
vendor/golang.org/x/text/unicode/bidi/tables11.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.13 && !go1.14
3 4
 // +build go1.13,!go1.14
4 5
 
5 6
 package bidi

+ 1
- 0
vendor/golang.org/x/text/unicode/bidi/tables12.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.14 && !go1.16
3 4
 // +build go1.14,!go1.16
4 5
 
5 6
 package bidi

+ 1
- 0
vendor/golang.org/x/text/unicode/bidi/tables13.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.16
3 4
 // +build go1.16
4 5
 
5 6
 package bidi

+ 1
- 0
vendor/golang.org/x/text/unicode/bidi/tables9.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build !go1.10
3 4
 // +build !go1.10
4 5
 
5 6
 package bidi

+ 1
- 0
vendor/golang.org/x/text/unicode/norm/tables10.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.10 && !go1.13
3 4
 // +build go1.10,!go1.13
4 5
 
5 6
 package norm

+ 1
- 0
vendor/golang.org/x/text/unicode/norm/tables11.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.13 && !go1.14
3 4
 // +build go1.13,!go1.14
4 5
 
5 6
 package norm

+ 1
- 0
vendor/golang.org/x/text/unicode/norm/tables12.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.14 && !go1.16
3 4
 // +build go1.14,!go1.16
4 5
 
5 6
 package norm

+ 1
- 0
vendor/golang.org/x/text/unicode/norm/tables13.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.16
3 4
 // +build go1.16
4 5
 
5 6
 package norm

+ 1
- 0
vendor/golang.org/x/text/unicode/norm/tables9.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build !go1.10
3 4
 // +build !go1.10
4 5
 
5 6
 package norm

+ 1
- 0
vendor/golang.org/x/text/width/tables10.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.10 && !go1.13
3 4
 // +build go1.10,!go1.13
4 5
 
5 6
 package width

+ 1
- 0
vendor/golang.org/x/text/width/tables11.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.13 && !go1.14
3 4
 // +build go1.13,!go1.14
4 5
 
5 6
 package width

+ 1
- 0
vendor/golang.org/x/text/width/tables12.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.14 && !go1.16
3 4
 // +build go1.14,!go1.16
4 5
 
5 6
 package width

+ 1
- 0
vendor/golang.org/x/text/width/tables13.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build go1.16
3 4
 // +build go1.16
4 5
 
5 6
 package width

+ 1
- 0
vendor/golang.org/x/text/width/tables9.0.0.go View File

@@ -1,5 +1,6 @@
1 1
 // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2 2
 
3
+//go:build !go1.10
3 4
 // +build !go1.10
4 5
 
5 6
 package width

+ 1
- 1
vendor/modules.txt View File

@@ -77,7 +77,7 @@ golang.org/x/sys/unix
77 77
 golang.org/x/sys/windows
78 78
 # golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
79 79
 golang.org/x/term
80
-# golang.org/x/text v0.3.4
80
+# golang.org/x/text v0.3.6
81 81
 ## explicit
82 82
 golang.org/x/text/cases
83 83
 golang.org/x/text/internal

Loading…
Cancel
Save