mw.mx raw

   1  package mw
   2  
   3  type MW struct {
   4  	S string
   5  	I int
   6  }
   7  
   8  func New(s string) MW {
   9  	w := MW{S: s}
  10  	for w.I < len(s) && s[w.I] != '[' {
  11  		w.I++
  12  	}
  13  	if w.I < len(s) {
  14  		w.I++
  15  	}
  16  	return w
  17  }
  18  
  19  func (w *MW) Sep() {
  20  	for w.I < len(w.S) {
  21  		c := w.S[w.I]
  22  		if c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != ',' {
  23  			break
  24  		}
  25  		w.I++
  26  	}
  27  }
  28  
  29  func (w *MW) Str() string {
  30  	w.Sep()
  31  	if w.I >= len(w.S) || w.S[w.I] != '"' {
  32  		return ""
  33  	}
  34  	w.I++
  35  	start := w.I
  36  	for w.I < len(w.S) && w.S[w.I] != '"' {
  37  		if w.S[w.I] == '\\' {
  38  			w.I++
  39  		}
  40  		w.I++
  41  	}
  42  	if w.I >= len(w.S) {
  43  		return ""
  44  	}
  45  	result := w.S[start:w.I]
  46  	w.I++
  47  	return result
  48  }
  49  
  50  func (w *MW) Num() int64 {
  51  	w.Sep()
  52  	neg := false
  53  	if w.I < len(w.S) && w.S[w.I] == '-' {
  54  		neg = true
  55  		w.I++
  56  	}
  57  	var n int64
  58  	for w.I < len(w.S) && w.S[w.I] >= '0' && w.S[w.I] <= '9' {
  59  		n = n*10 + int64(w.S[w.I]-'0')
  60  		w.I++
  61  	}
  62  	if neg {
  63  		n = -n
  64  	}
  65  	return n
  66  }
  67  
  68  func (w *MW) Raw() string {
  69  	w.Sep()
  70  	start := w.I
  71  	w.I = Skipval(w.S, w.I)
  72  	if w.I < 0 {
  73  		w.I = len(w.S)
  74  		return ""
  75  	}
  76  	return w.S[start:w.I]
  77  }
  78  
  79  func (w *MW) Strs() []string {
  80  	w.Sep()
  81  	if w.I >= len(w.S) || w.S[w.I] != '[' {
  82  		return nil
  83  	}
  84  	w.I++
  85  	var out []string
  86  	for {
  87  		w.Sep()
  88  		if w.I >= len(w.S) || w.S[w.I] == ']' {
  89  			if w.I < len(w.S) {
  90  				w.I++
  91  			}
  92  			return out
  93  		}
  94  		s := w.Str()
  95  		out = append(out, s)
  96  	}
  97  }
  98  
  99  func Skipval(s string, i int) int {
 100  	if i >= len(s) {
 101  		return -1
 102  	}
 103  	c := s[i]
 104  	if c == '"' {
 105  		i++
 106  		for i < len(s) && s[i] != '"' {
 107  			if s[i] == '\\' {
 108  				i++
 109  			}
 110  			i++
 111  		}
 112  		if i >= len(s) {
 113  			return -1
 114  		}
 115  		return i + 1
 116  	}
 117  	if c == '{' || c == '[' {
 118  		open := c
 119  		close := byte('}')
 120  		if open == '[' {
 121  			close = ']'
 122  		}
 123  		depth := 1
 124  		i++
 125  		for i < len(s) && depth > 0 {
 126  			if s[i] == '"' {
 127  				i++
 128  				for i < len(s) && s[i] != '"' {
 129  					if s[i] == '\\' {
 130  						i++
 131  					}
 132  					i++
 133  				}
 134  				if i >= len(s) {
 135  					return -1
 136  				}
 137  				i++
 138  				continue
 139  			}
 140  			if s[i] == open {
 141  				depth++
 142  			} else if s[i] == close {
 143  				depth--
 144  				if depth == 0 {
 145  					return i + 1
 146  				}
 147  			}
 148  			i++
 149  		}
 150  		return -1
 151  	}
 152  	for i < len(s) {
 153  		c := s[i]
 154  		if c == ',' || c == ']' || c == '}' {
 155  			return i
 156  		}
 157  		i++
 158  	}
 159  	return i
 160  }
 161