OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [bytes/] [bytes.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package bytes implements functions for the manipulation of byte slices.
6
// It is analogous to the facilities of the strings package.
7
package bytes
8
 
9
import (
10
        "unicode"
11
        "unicode/utf8"
12
)
13
 
14
// Compare returns an integer comparing the two byte arrays lexicographically.
15
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b
16
func Compare(a, b []byte) int {
17
        m := len(a)
18
        if m > len(b) {
19
                m = len(b)
20
        }
21
        for i, ac := range a[0:m] {
22
                bc := b[i]
23
                switch {
24
                case ac > bc:
25
                        return 1
26
                case ac < bc:
27
                        return -1
28
                }
29
        }
30
        switch {
31
        case len(a) < len(b):
32
                return -1
33
        case len(a) > len(b):
34
                return 1
35
        }
36
        return 0
37
}
38
 
39
// Equal returns a boolean reporting whether a == b.
40
func Equal(a, b []byte) bool
41
 
42
func equalPortable(a, b []byte) bool {
43
        if len(a) != len(b) {
44
                return false
45
        }
46
        for i, c := range a {
47
                if c != b[i] {
48
                        return false
49
                }
50
        }
51
        return true
52
}
53
 
54
// explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes),
55
// up to a maximum of n byte arrays. Invalid UTF-8 sequences are chopped into individual bytes.
56
func explode(s []byte, n int) [][]byte {
57
        if n <= 0 {
58
                n = len(s)
59
        }
60
        a := make([][]byte, n)
61
        var size int
62
        na := 0
63
        for len(s) > 0 {
64
                if na+1 >= n {
65
                        a[na] = s
66
                        na++
67
                        break
68
                }
69
                _, size = utf8.DecodeRune(s)
70
                a[na] = s[0:size]
71
                s = s[size:]
72
                na++
73
        }
74
        return a[0:na]
75
}
76
 
77
// Count counts the number of non-overlapping instances of sep in s.
78
func Count(s, sep []byte) int {
79
        n := len(sep)
80
        if n == 0 {
81
                return utf8.RuneCount(s) + 1
82
        }
83
        if n > len(s) {
84
                return 0
85
        }
86
        count := 0
87
        c := sep[0]
88
        i := 0
89
        t := s[:len(s)-n+1]
90
        for i < len(t) {
91
                if t[i] != c {
92
                        o := IndexByte(t[i:], c)
93
                        if o < 0 {
94
                                break
95
                        }
96
                        i += o
97
                }
98
                if n == 1 || Equal(s[i:i+n], sep) {
99
                        count++
100
                        i += n
101
                        continue
102
                }
103
                i++
104
        }
105
        return count
106
}
107
 
108
// Contains returns whether subslice is within b.
109
func Contains(b, subslice []byte) bool {
110
        return Index(b, subslice) != -1
111
}
112
 
113
// Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
114
func Index(s, sep []byte) int {
115
        n := len(sep)
116
        if n == 0 {
117
                return 0
118
        }
119
        if n > len(s) {
120
                return -1
121
        }
122
        c := sep[0]
123
        if n == 1 {
124
                return IndexByte(s, c)
125
        }
126
        i := 0
127
        t := s[:len(s)-n+1]
128
        for i < len(t) {
129
                if t[i] != c {
130
                        o := IndexByte(t[i:], c)
131
                        if o < 0 {
132
                                break
133
                        }
134
                        i += o
135
                }
136
                if Equal(s[i:i+n], sep) {
137
                        return i
138
                }
139
                i++
140
        }
141
        return -1
142
}
143
 
144
func indexBytePortable(s []byte, c byte) int {
145
        for i, b := range s {
146
                if b == c {
147
                        return i
148
                }
149
        }
150
        return -1
151
}
152
 
153
// LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
154
func LastIndex(s, sep []byte) int {
155
        n := len(sep)
156
        if n == 0 {
157
                return len(s)
158
        }
159
        c := sep[0]
160
        for i := len(s) - n; i >= 0; i-- {
161
                if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) {
162
                        return i
163
                }
164
        }
165
        return -1
166
}
167
 
168
// IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points.
169
// It returns the byte index of the first occurrence in s of the given rune.
170
// It returns -1 if rune is not present in s.
171
func IndexRune(s []byte, r rune) int {
172
        for i := 0; i < len(s); {
173
                r1, size := utf8.DecodeRune(s[i:])
174
                if r == r1 {
175
                        return i
176
                }
177
                i += size
178
        }
179
        return -1
180
}
181
 
182
// IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points.
183
// It returns the byte index of the first occurrence in s of any of the Unicode
184
// code points in chars.  It returns -1 if chars is empty or if there is no code
185
// point in common.
186
func IndexAny(s []byte, chars string) int {
187
        if len(chars) > 0 {
188
                var r rune
189
                var width int
190
                for i := 0; i < len(s); i += width {
191
                        r = rune(s[i])
192
                        if r < utf8.RuneSelf {
193
                                width = 1
194
                        } else {
195
                                r, width = utf8.DecodeRune(s[i:])
196
                        }
197
                        for _, ch := range chars {
198
                                if r == ch {
199
                                        return i
200
                                }
201
                        }
202
                }
203
        }
204
        return -1
205
}
206
 
207
// LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code
208
// points.  It returns the byte index of the last occurrence in s of any of
209
// the Unicode code points in chars.  It returns -1 if chars is empty or if
210
// there is no code point in common.
211
func LastIndexAny(s []byte, chars string) int {
212
        if len(chars) > 0 {
213
                for i := len(s); i > 0; {
214
                        r, size := utf8.DecodeLastRune(s[0:i])
215
                        i -= size
216
                        for _, ch := range chars {
217
                                if r == ch {
218
                                        return i
219
                                }
220
                        }
221
                }
222
        }
223
        return -1
224
}
225
 
226
// Generic split: splits after each instance of sep,
227
// including sepSave bytes of sep in the subarrays.
228
func genSplit(s, sep []byte, sepSave, n int) [][]byte {
229
        if n == 0 {
230
                return nil
231
        }
232
        if len(sep) == 0 {
233
                return explode(s, n)
234
        }
235
        if n < 0 {
236
                n = Count(s, sep) + 1
237
        }
238
        c := sep[0]
239
        start := 0
240
        a := make([][]byte, n)
241
        na := 0
242
        for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
243
                if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) {
244
                        a[na] = s[start : i+sepSave]
245
                        na++
246
                        start = i + len(sep)
247
                        i += len(sep) - 1
248
                }
249
        }
250
        a[na] = s[start:]
251
        return a[0 : na+1]
252
}
253
 
254
// SplitN slices s into subslices separated by sep and returns a slice of
255
// the subslices between those separators.
256
// If sep is empty, SplitN splits after each UTF-8 sequence.
257
// The count determines the number of subslices to return:
258
//   n > 0: at most n subslices; the last subslice will be the unsplit remainder.
259
//   n == 0: the result is nil (zero subslices)
260
//   n < 0: all subslices
261
func SplitN(s, sep []byte, n int) [][]byte { return genSplit(s, sep, 0, n) }
262
 
263
// SplitAfterN slices s into subslices after each instance of sep and
264
// returns a slice of those subslices.
265
// If sep is empty, SplitAfterN splits after each UTF-8 sequence.
266
// The count determines the number of subslices to return:
267
//   n > 0: at most n subslices; the last subslice will be the unsplit remainder.
268
//   n == 0: the result is nil (zero subslices)
269
//   n < 0: all subslices
270
func SplitAfterN(s, sep []byte, n int) [][]byte {
271
        return genSplit(s, sep, len(sep), n)
272
}
273
 
274
// Split slices s into all subslices separated by sep and returns a slice of
275
// the subslices between those separators.
276
// If sep is empty, Split splits after each UTF-8 sequence.
277
// It is equivalent to SplitN with a count of -1.
278
func Split(s, sep []byte) [][]byte { return genSplit(s, sep, 0, -1) }
279
 
280
// SplitAfter slices s into all subslices after each instance of sep and
281
// returns a slice of those subslices.
282
// If sep is empty, SplitAfter splits after each UTF-8 sequence.
283
// It is equivalent to SplitAfterN with a count of -1.
284
func SplitAfter(s, sep []byte) [][]byte {
285
        return genSplit(s, sep, len(sep), -1)
286
}
287
 
288
// Fields splits the array s around each instance of one or more consecutive white space
289
// characters, returning a slice of subarrays of s or an empty list if s contains only white space.
290
func Fields(s []byte) [][]byte {
291
        return FieldsFunc(s, unicode.IsSpace)
292
}
293
 
294
// FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
295
// It splits the array s at each run of code points c satisfying f(c) and
296
// returns a slice of subarrays of s.  If no code points in s satisfy f(c), an
297
// empty slice is returned.
298
func FieldsFunc(s []byte, f func(rune) bool) [][]byte {
299
        n := 0
300
        inField := false
301
        for i := 0; i < len(s); {
302
                r, size := utf8.DecodeRune(s[i:])
303
                wasInField := inField
304
                inField = !f(r)
305
                if inField && !wasInField {
306
                        n++
307
                }
308
                i += size
309
        }
310
 
311
        a := make([][]byte, n)
312
        na := 0
313
        fieldStart := -1
314
        for i := 0; i <= len(s) && na < n; {
315
                r, size := utf8.DecodeRune(s[i:])
316
                if fieldStart < 0 && size > 0 && !f(r) {
317
                        fieldStart = i
318
                        i += size
319
                        continue
320
                }
321
                if fieldStart >= 0 && (size == 0 || f(r)) {
322
                        a[na] = s[fieldStart:i]
323
                        na++
324
                        fieldStart = -1
325
                }
326
                if size == 0 {
327
                        break
328
                }
329
                i += size
330
        }
331
        return a[0:na]
332
}
333
 
334
// Join concatenates the elements of a to create a single byte array.   The separator
335
// sep is placed between elements in the resulting array.
336
func Join(a [][]byte, sep []byte) []byte {
337
        if len(a) == 0 {
338
                return []byte{}
339
        }
340
        if len(a) == 1 {
341
                return a[0]
342
        }
343
        n := len(sep) * (len(a) - 1)
344
        for i := 0; i < len(a); i++ {
345
                n += len(a[i])
346
        }
347
 
348
        b := make([]byte, n)
349
        bp := copy(b, a[0])
350
        for _, s := range a[1:] {
351
                bp += copy(b[bp:], sep)
352
                bp += copy(b[bp:], s)
353
        }
354
        return b
355
}
356
 
357
// HasPrefix tests whether the byte array s begins with prefix.
358
func HasPrefix(s, prefix []byte) bool {
359
        return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
360
}
361
 
362
// HasSuffix tests whether the byte array s ends with suffix.
363
func HasSuffix(s, suffix []byte) bool {
364
        return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):], suffix)
365
}
366
 
367
// Map returns a copy of the byte array s with all its characters modified
368
// according to the mapping function. If mapping returns a negative value, the character is
369
// dropped from the string with no replacement.  The characters in s and the
370
// output are interpreted as UTF-8-encoded Unicode code points.
371
func Map(mapping func(r rune) rune, s []byte) []byte {
372
        // In the worst case, the array can grow when mapped, making
373
        // things unpleasant.  But it's so rare we barge in assuming it's
374
        // fine.  It could also shrink but that falls out naturally.
375
        maxbytes := len(s) // length of b
376
        nbytes := 0        // number of bytes encoded in b
377
        b := make([]byte, maxbytes)
378
        for i := 0; i < len(s); {
379
                wid := 1
380
                r := rune(s[i])
381
                if r >= utf8.RuneSelf {
382
                        r, wid = utf8.DecodeRune(s[i:])
383
                }
384
                r = mapping(r)
385
                if r >= 0 {
386
                        if nbytes+utf8.RuneLen(r) > maxbytes {
387
                                // Grow the buffer.
388
                                maxbytes = maxbytes*2 + utf8.UTFMax
389
                                nb := make([]byte, maxbytes)
390
                                copy(nb, b[0:nbytes])
391
                                b = nb
392
                        }
393
                        nbytes += utf8.EncodeRune(b[nbytes:maxbytes], r)
394
                }
395
                i += wid
396
        }
397
        return b[0:nbytes]
398
}
399
 
400
// Repeat returns a new byte slice consisting of count copies of b.
401
func Repeat(b []byte, count int) []byte {
402
        nb := make([]byte, len(b)*count)
403
        bp := 0
404
        for i := 0; i < count; i++ {
405
                for j := 0; j < len(b); j++ {
406
                        nb[bp] = b[j]
407
                        bp++
408
                }
409
        }
410
        return nb
411
}
412
 
413
// ToUpper returns a copy of the byte array s with all Unicode letters mapped to their upper case.
414
func ToUpper(s []byte) []byte { return Map(unicode.ToUpper, s) }
415
 
416
// ToUpper returns a copy of the byte array s with all Unicode letters mapped to their lower case.
417
func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
418
 
419
// ToTitle returns a copy of the byte array s with all Unicode letters mapped to their title case.
420
func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }
421
 
422
// ToUpperSpecial returns a copy of the byte array s with all Unicode letters mapped to their
423
// upper case, giving priority to the special casing rules.
424
func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte {
425
        return Map(func(r rune) rune { return _case.ToUpper(r) }, s)
426
}
427
 
428
// ToLowerSpecial returns a copy of the byte array s with all Unicode letters mapped to their
429
// lower case, giving priority to the special casing rules.
430
func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte {
431
        return Map(func(r rune) rune { return _case.ToLower(r) }, s)
432
}
433
 
434
// ToTitleSpecial returns a copy of the byte array s with all Unicode letters mapped to their
435
// title case, giving priority to the special casing rules.
436
func ToTitleSpecial(_case unicode.SpecialCase, s []byte) []byte {
437
        return Map(func(r rune) rune { return _case.ToTitle(r) }, s)
438
}
439
 
440
// isSeparator reports whether the rune could mark a word boundary.
441
// TODO: update when package unicode captures more of the properties.
442
func isSeparator(r rune) bool {
443
        // ASCII alphanumerics and underscore are not separators
444
        if r <= 0x7F {
445
                switch {
446
                case '0' <= r && r <= '9':
447
                        return false
448
                case 'a' <= r && r <= 'z':
449
                        return false
450
                case 'A' <= r && r <= 'Z':
451
                        return false
452
                case r == '_':
453
                        return false
454
                }
455
                return true
456
        }
457
        // Letters and digits are not separators
458
        if unicode.IsLetter(r) || unicode.IsDigit(r) {
459
                return false
460
        }
461
        // Otherwise, all we can do for now is treat spaces as separators.
462
        return unicode.IsSpace(r)
463
}
464
 
465
// BUG(r): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
466
 
467
// Title returns a copy of s with all Unicode letters that begin words
468
// mapped to their title case.
469
func Title(s []byte) []byte {
470
        // Use a closure here to remember state.
471
        // Hackish but effective. Depends on Map scanning in order and calling
472
        // the closure once per rune.
473
        prev := ' '
474
        return Map(
475
                func(r rune) rune {
476
                        if isSeparator(prev) {
477
                                prev = r
478
                                return unicode.ToTitle(r)
479
                        }
480
                        prev = r
481
                        return r
482
                },
483
                s)
484
}
485
 
486
// TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8-encoded
487
// Unicode code points c that satisfy f(c).
488
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte {
489
        i := indexFunc(s, f, false)
490
        if i == -1 {
491
                return nil
492
        }
493
        return s[i:]
494
}
495
 
496
// TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
497
// encoded Unicode code points c that satisfy f(c).
498
func TrimRightFunc(s []byte, f func(r rune) bool) []byte {
499
        i := lastIndexFunc(s, f, false)
500
        if i >= 0 && s[i] >= utf8.RuneSelf {
501
                _, wid := utf8.DecodeRune(s[i:])
502
                i += wid
503
        } else {
504
                i++
505
        }
506
        return s[0:i]
507
}
508
 
509
// TrimFunc returns a subslice of s by slicing off all leading and trailing
510
// UTF-8-encoded Unicode code points c that satisfy f(c).
511
func TrimFunc(s []byte, f func(r rune) bool) []byte {
512
        return TrimRightFunc(TrimLeftFunc(s, f), f)
513
}
514
 
515
// IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
516
// It returns the byte index in s of the first Unicode
517
// code point satisfying f(c), or -1 if none do.
518
func IndexFunc(s []byte, f func(r rune) bool) int {
519
        return indexFunc(s, f, true)
520
}
521
 
522
// LastIndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
523
// It returns the byte index in s of the last Unicode
524
// code point satisfying f(c), or -1 if none do.
525
func LastIndexFunc(s []byte, f func(r rune) bool) int {
526
        return lastIndexFunc(s, f, true)
527
}
528
 
529
// indexFunc is the same as IndexFunc except that if
530
// truth==false, the sense of the predicate function is
531
// inverted.
532
func indexFunc(s []byte, f func(r rune) bool, truth bool) int {
533
        start := 0
534
        for start < len(s) {
535
                wid := 1
536
                r := rune(s[start])
537
                if r >= utf8.RuneSelf {
538
                        r, wid = utf8.DecodeRune(s[start:])
539
                }
540
                if f(r) == truth {
541
                        return start
542
                }
543
                start += wid
544
        }
545
        return -1
546
}
547
 
548
// lastIndexFunc is the same as LastIndexFunc except that if
549
// truth==false, the sense of the predicate function is
550
// inverted.
551
func lastIndexFunc(s []byte, f func(r rune) bool, truth bool) int {
552
        for i := len(s); i > 0; {
553
                r, size := utf8.DecodeLastRune(s[0:i])
554
                i -= size
555
                if f(r) == truth {
556
                        return i
557
                }
558
        }
559
        return -1
560
}
561
 
562
func makeCutsetFunc(cutset string) func(r rune) bool {
563
        return func(r rune) bool {
564
                for _, c := range cutset {
565
                        if c == r {
566
                                return true
567
                        }
568
                }
569
                return false
570
        }
571
}
572
 
573
// Trim returns a subslice of s by slicing off all leading and
574
// trailing UTF-8-encoded Unicode code points contained in cutset.
575
func Trim(s []byte, cutset string) []byte {
576
        return TrimFunc(s, makeCutsetFunc(cutset))
577
}
578
 
579
// TrimLeft returns a subslice of s by slicing off all leading
580
// UTF-8-encoded Unicode code points contained in cutset.
581
func TrimLeft(s []byte, cutset string) []byte {
582
        return TrimLeftFunc(s, makeCutsetFunc(cutset))
583
}
584
 
585
// TrimRight returns a subslice of s by slicing off all trailing
586
// UTF-8-encoded Unicode code points that are contained in cutset.
587
func TrimRight(s []byte, cutset string) []byte {
588
        return TrimRightFunc(s, makeCutsetFunc(cutset))
589
}
590
 
591
// TrimSpace returns a subslice of s by slicing off all leading and
592
// trailing white space, as defined by Unicode.
593
func TrimSpace(s []byte) []byte {
594
        return TrimFunc(s, unicode.IsSpace)
595
}
596
 
597
// Runes returns a slice of runes (Unicode code points) equivalent to s.
598
func Runes(s []byte) []rune {
599
        t := make([]rune, utf8.RuneCount(s))
600
        i := 0
601
        for len(s) > 0 {
602
                r, l := utf8.DecodeRune(s)
603
                t[i] = r
604
                i++
605
                s = s[l:]
606
        }
607
        return t
608
}
609
 
610
// Replace returns a copy of the slice s with the first n
611
// non-overlapping instances of old replaced by new.
612
// If n < 0, there is no limit on the number of replacements.
613
func Replace(s, old, new []byte, n int) []byte {
614
        m := 0
615
        if n != 0 {
616
                // Compute number of replacements.
617
                m = Count(s, old)
618
        }
619
        if m == 0 {
620
                // Nothing to do. Just copy.
621
                t := make([]byte, len(s))
622
                copy(t, s)
623
                return t
624
        }
625
        if n < 0 || m < n {
626
                n = m
627
        }
628
 
629
        // Apply replacements to buffer.
630
        t := make([]byte, len(s)+n*(len(new)-len(old)))
631
        w := 0
632
        start := 0
633
        for i := 0; i < n; i++ {
634
                j := start
635
                if len(old) == 0 {
636
                        if i > 0 {
637
                                _, wid := utf8.DecodeRune(s[start:])
638
                                j += wid
639
                        }
640
                } else {
641
                        j += Index(s[start:], old)
642
                }
643
                w += copy(t[w:], s[start:j])
644
                w += copy(t[w:], new)
645
                start = j + len(old)
646
        }
647
        w += copy(t[w:], s[start:])
648
        return t[0:w]
649
}
650
 
651
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
652
// are equal under Unicode case-folding.
653
func EqualFold(s, t []byte) bool {
654
        for len(s) != 0 && len(t) != 0 {
655
                // Extract first rune from each.
656
                var sr, tr rune
657
                if s[0] < utf8.RuneSelf {
658
                        sr, s = rune(s[0]), s[1:]
659
                } else {
660
                        r, size := utf8.DecodeRune(s)
661
                        sr, s = r, s[size:]
662
                }
663
                if t[0] < utf8.RuneSelf {
664
                        tr, t = rune(t[0]), t[1:]
665
                } else {
666
                        r, size := utf8.DecodeRune(t)
667
                        tr, t = r, t[size:]
668
                }
669
 
670
                // If they match, keep going; if not, return false.
671
 
672
                // Easy case.
673
                if tr == sr {
674
                        continue
675
                }
676
 
677
                // Make sr < tr to simplify what follows.
678
                if tr < sr {
679
                        tr, sr = sr, tr
680
                }
681
                // Fast check for ASCII.
682
                if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
683
                        // ASCII, and sr is upper case.  tr must be lower case.
684
                        if tr == sr+'a'-'A' {
685
                                continue
686
                        }
687
                        return false
688
                }
689
 
690
                // General case.  SimpleFold(x) returns the next equivalent rune > x
691
                // or wraps around to smaller values.
692
                r := unicode.SimpleFold(sr)
693
                for r != sr && r < tr {
694
                        r = unicode.SimpleFold(r)
695
                }
696
                if r == tr {
697
                        continue
698
                }
699
                return false
700
        }
701
 
702
        // One string is empty.  Are both?
703
        return len(s) == len(t)
704
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.