summaryrefslogtreecommitdiff
path: root/src/lib/net/parse.go
blob: a1cf10c4a11a51df1f1a4db506e20b8fd31b969f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Simple file i/o and string manipulation, to avoid
// depending on strconv and bufio.

package net

import (
	"io";
	"os";
)

type _File struct {
	fd *os.FD;
	data []byte;
}

func (f *_File) Close() {
	f.fd.Close()
}

func (f *_File) GetLineFromData() (s string, ok bool) {
	data := f.data;
	for i := 0; i < len(data); i++ {
		if data[i] == '\n' {
			s = string(data[0:i]);
			ok = true;
			// move data
			i++;
			n := len(data) - i;
			for j := 0; j < n; j++ {
				data[j] = data[i+j];
			}
			f.data = data[0:n];
			return
		}
	}
	return
}

func (f *_File) ReadLine() (s string, ok bool) {
	if s, ok = f.GetLineFromData(); ok {
		return
	}
	if len(f.data) < cap(f.data) {
		ln := len(f.data);
		n, err := io.Readn(f.fd, f.data[ln:cap(f.data)]);
		if n >= 0 {
			f.data = f.data[0:ln+n];
		}
	}
	s, ok = f.GetLineFromData();
	return
}

func _Open(name string) *_File {
	fd, err := os.Open(name, os.O_RDONLY, 0);
	if err != nil {
		return nil
	}
	return &_File(fd, make([]byte, 1024)[0:0]);
}

func _ByteIndex(s string, c byte) int {
	for i := 0; i < len(s); i++ {
		if s[i] == c {
			return i
		}
	}
	return -1
}

// Count occurrences in s of any bytes in t.
func _CountAnyByte(s string, t string) int {
	n := 0;
	for i := 0; i < len(s); i++ {
		if _ByteIndex(t, s[i]) >= 0 {
			n++;
		}
	}
	return n
}

// Split s at any bytes in t.
func _SplitAtBytes(s string, t string) []string {
	a := make([]string, 1+_CountAnyByte(s, t));
	n := 0;
	last := 0;
	for i := 0; i < len(s); i++ {
		if _ByteIndex(t, s[i]) >= 0 {
			if last < i {
				a[n] = string(s[last:i]);
				n++;
			}
			last = i+1;
		}
	}
	if last < len(s) {
		a[n] = string(s[last:len(s)]);
		n++;
	}
	return a[0:n];
}

func _GetFields(s string) []string {
	return _SplitAtBytes(s, " \r\t\n");
}

// Bigger than we need, not too big to worry about overflow
const _Big = 0xFFFFFF

// Decimal to integer starting at &s[i0].
// Returns number, new offset, success.
func _Dtoi(s string, i0 int) (n int, i int, ok bool) {
	n = 0;
	for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
		n = n*10 + int(s[i] - '0');
		if n >= _Big {
			return 0, i, false
		}
	}
	if i == i0 {
		return 0, i, false
	}
	return n, i, true
}

// Hexadecimal to integer starting at &s[i0].
// Returns number, new offset, success.
func _Xtoi(s string, i0 int) (n int, i int, ok bool) {
	n = 0;
	for i = i0; i < len(s); i++ {
		if '0' <= s[i] && s[i] <= '9' {
			n *= 16;
			n += int(s[i] - '0')
		} else if 'a' <= s[i] && s[i] <= 'f' {
			n *= 16;
			n += int(s[i] - 'a') + 10
		} else if 'A' <= s[i] && s[i] <= 'F' {
			n *= 16;
			n += int(s[i] -'A') + 10
		} else {
			break
		}
		if n >= _Big {
			return 0, i, false
		}
	}
	if i == i0 {
		return 0, i, false
	}
	return n, i, true
}