~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/regexp/regexp.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
// general syntax used by Perl, Python, and other languages.
9
9
// More precisely, it is the syntax accepted by RE2 and described at
10
10
// http://code.google.com/p/re2/wiki/Syntax, except for \C.
 
11
// For an overview of the syntax, run
 
12
//   godoc regexp/syntax
11
13
//
12
14
// All characters are UTF-8-encoded code points.
13
15
//
27
29
// of bytes; return values are adjusted as appropriate.
28
30
//
29
31
// If 'Submatch' is present, the return value is a slice identifying the
30
 
// successive submatches of the expression.  Submatches are matches of
31
 
// parenthesized subexpressions within the regular expression, numbered from
32
 
// left to right in order of opening parenthesis.  Submatch 0 is the match of
33
 
// the entire expression, submatch 1 the match of the first parenthesized
34
 
// subexpression, and so on.
 
32
// successive submatches of the expression. Submatches are matches of
 
33
// parenthesized subexpressions (also known as capturing groups) within the
 
34
// regular expression, numbered from left to right in order of opening
 
35
// parenthesis. Submatch 0 is the match of the entire expression, submatch 1
 
36
// the match of the first parenthesized subexpression, and so on.
35
37
//
36
38
// If 'Index' is present, matches and submatches are identified by byte index
37
39
// pairs within the input string: result[2*n:2*n+1] identifies the indexes of
130
132
        return compile(expr, syntax.POSIX, true)
131
133
}
132
134
 
 
135
// Longest makes future searches prefer the leftmost-longest match.
 
136
// That is, when matching against text, the regexp returns a match that
 
137
// begins as early as possible in the input (leftmost), and among those
 
138
// it chooses a match that is as long as possible.
 
139
func (re *Regexp) Longest() {
 
140
        re.longest = true
 
141
}
 
142
 
133
143
func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
134
144
        re, err := syntax.Parse(expr, mode)
135
145
        if err != nil {
387
397
// MatchReader checks whether a textual regular expression matches the text
388
398
// read by the RuneReader.  More complicated queries need to use Compile and
389
399
// the full Regexp interface.
390
 
func MatchReader(pattern string, r io.RuneReader) (matched bool, error error) {
 
400
func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) {
391
401
        re, err := Compile(pattern)
392
402
        if err != nil {
393
403
                return false, err
398
408
// MatchString checks whether a textual regular expression
399
409
// matches a string.  More complicated queries need
400
410
// to use Compile and the full Regexp interface.
401
 
func MatchString(pattern string, s string) (matched bool, error error) {
 
411
func MatchString(pattern string, s string) (matched bool, err error) {
402
412
        re, err := Compile(pattern)
403
413
        if err != nil {
404
414
                return false, err
409
419
// Match checks whether a textual regular expression
410
420
// matches a byte slice.  More complicated queries need
411
421
// to use Compile and the full Regexp interface.
412
 
func Match(pattern string, b []byte) (matched bool, error error) {
 
422
func Match(pattern string, b []byte) (matched bool, err error) {
413
423
        re, err := Compile(pattern)
414
424
        if err != nil {
415
425
                return false, err
441
451
}
442
452
 
443
453
// ReplaceAllStringFunc returns a copy of src in which all matches of the
444
 
// Regexp have been replaced by the return value of of function repl applied
 
454
// Regexp have been replaced by the return value of function repl applied
445
455
// to the matched substring.  The replacement returned by repl is substituted
446
456
// directly, without using Expand.
447
457
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
539
549
}
540
550
 
541
551
// ReplaceAllFunc returns a copy of src in which all matches of the
542
 
// Regexp have been replaced by the return value of of function repl applied
 
552
// Regexp have been replaced by the return value of function repl applied
543
553
// to the matched byte slice.  The replacement returned by repl is substituted
544
554
// directly, without using Expand.
545
555
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte {
686
696
 
687
697
// FindReaderIndex returns a two-element slice of integers defining the
688
698
// location of the leftmost match of the regular expression in text read from
689
 
// the RuneReader.  The match itself is at s[loc[0]:loc[1]].  A return
690
 
// value of nil indicates no match.
 
699
// the RuneReader.  The match text was found in the input stream at
 
700
// byte offset loc[0] through loc[1]-1.
 
701
// A return value of nil indicates no match.
691
702
func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) {
692
703
        a := re.doExecute(r, nil, "", 0, 2)
693
704
        if a == nil {
719
730
// append, Expand replaces variables in the template with corresponding
720
731
// matches drawn from src.  The match slice should have been returned by
721
732
// FindSubmatchIndex.
722
 
// 
 
733
//
723
734
// In the template, a variable is denoted by a substring of the form
724
735
// $name or ${name}, where name is a non-empty sequence of letters,
725
736
// digits, and underscores.  A purely numeric name like $1 refers to
727
738
// capturing parentheses named with the (?P<name>...) syntax.  A
728
739
// reference to an out of range or unmatched index or a name that is not
729
740
// present in the regular expression is replaced with an empty slice.
730
 
// 
 
741
//
731
742
// In the $name form, name is taken to be as long as possible: $1x is
732
743
// equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0.
733
 
// 
 
744
//
734
745
// To insert a literal $ in the output, use $$ in the template.
735
746
func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte {
736
747
        return re.expand(dst, string(template), src, "", match)
766
777
                }
767
778
                template = rest
768
779
                if num >= 0 {
769
 
                        if 2*num+1 < len(match) {
 
780
                        if 2*num+1 < len(match) && match[2*num] >= 0 {
770
781
                                if bsrc != nil {
771
782
                                        dst = append(dst, bsrc[match[2*num]:match[2*num+1]]...)
772
783
                                } else {
1047
1058
        }
1048
1059
        return result
1049
1060
}
 
1061
 
 
1062
// Split slices s into substrings separated by the expression and returns a slice of
 
1063
// the substrings between those expression matches.
 
1064
//
 
1065
// The slice returned by this method consists of all the substrings of s
 
1066
// not contained in the slice returned by FindAllString. When called on an expression
 
1067
// that contains no metacharacters, it is equivalent to strings.SplitN.
 
1068
//
 
1069
// Example:
 
1070
//   s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
 
1071
//   // s: ["", "b", "b", "c", "cadaaae"]
 
1072
//
 
1073
// The count determines the number of substrings to return:
 
1074
//   n > 0: at most n substrings; the last substring will be the unsplit remainder.
 
1075
//   n == 0: the result is nil (zero substrings)
 
1076
//   n < 0: all substrings
 
1077
func (re *Regexp) Split(s string, n int) []string {
 
1078
 
 
1079
        if n == 0 {
 
1080
                return nil
 
1081
        }
 
1082
 
 
1083
        if len(re.expr) > 0 && len(s) == 0 {
 
1084
                return []string{""}
 
1085
        }
 
1086
 
 
1087
        matches := re.FindAllStringIndex(s, n)
 
1088
        strings := make([]string, 0, len(matches))
 
1089
 
 
1090
        beg := 0
 
1091
        end := 0
 
1092
        for _, match := range matches {
 
1093
                if n > 0 && len(strings) >= n-1 {
 
1094
                        break
 
1095
                }
 
1096
 
 
1097
                end = match[0]
 
1098
                if match[1] != 0 {
 
1099
                        strings = append(strings, s[beg:end])
 
1100
                }
 
1101
                beg = match[1]
 
1102
        }
 
1103
 
 
1104
        if end != len(s) {
 
1105
                strings = append(strings, s[beg:])
 
1106
        }
 
1107
 
 
1108
        return strings
 
1109
}