-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStringList.go
357 lines (335 loc) · 7.87 KB
/
StringList.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
仿Delphi的通用类库
GStringList类似于TStringList
Autor: 不得闲
QQ:75492895
*/
package DxCommonLib
import (
"fmt"
"os"
"strings"
"io"
"bytes"
"bufio"
)
type (
IStrings interface {
Count() int
Strings(index int) string
SetStrings(index int, str string)
Text() string
SetText(text string)
LoadFromFile(fileName string)
SaveToFile(fileName string)
Add(str string)
Insert(Index int, str string)
Delete(index int)
AddStrings(strs IStrings)
AddSlice(strs []string)
Clear()
IndexOf(str string) int
AddPair(Name, Value string)
IndexOfName(Name string) int
ValueFromIndex(index int) string
ValueByName(Name string) string
Names(Index int) string
AsSlice() []string
}
LineBreakMode byte
GStringList struct {
strings []string
LineBreak LineBreakMode
UnknownCodeUseGbk bool //未知编码的时候采用GBK编码打开
}
)
const (
LBK_CRLF LineBreakMode = iota
LBK_CR
LBK_LF
)
func (lst *GStringList) LineBreakStr() string {
switch lst.LineBreak {
case LBK_CRLF:
return "\r\n"
case LBK_CR:
return "\r"
case LBK_LF:
return "\n"
default:
return "\r\n"
}
}
func (lst *GStringList) Count() int {
if lst.strings == nil {
return 0
}
return len(lst.strings)
}
func (lst *GStringList) Strings(index int) string {
if index >= 0 && index < len(lst.strings) {
return lst.strings[index]
}
return ""
}
func (lst *GStringList) SetStrings(index int, str string) {
if lst.strings == nil {
lst.strings = make([]string, 0, 64)
}
if index >= 0 && index < len(lst.strings) {
lst.strings[index] = str
}
}
func (lst *GStringList) Text() string {
if lst.Count() == 0 {
return ""
}
buffer := bytes.NewBuffer(make([]byte,0,1024))
count := lst.Count()
for i := 0;i<count;i++{
buffer.WriteString(lst.strings[i])
if i < count - 1{
buffer.WriteString(lst.LineBreakStr())
}
}
return FastByte2String(buffer.Bytes())
}
func (lst *GStringList) SetText(text string) {
lst.strings = strings.Split(text, lst.LineBreakStr())
}
func (lst *GStringList) LoadFromFile(fileName string) {
if finfo, err := os.Stat(fileName); err == nil && !finfo.IsDir() {
if file, err := os.Open(fileName); err == nil {
//先判定文件格式,是否为utf8或者utf16,去掉BOM
if lst.strings == nil{
lst.strings = make([]string,0,100)
}else{
lst.strings = lst.strings[:0]
}
defer file.Close()
var bt [3]byte
filecodeType := File_Code_Unknown
if _,err := file.Read(bt[:3]);err==nil{
if bt[0] == 0xFF && bt[1] == 0xFE { //UTF-16(Little Endian)
file.Seek(-1,io.SeekCurrent)
filecodeType = File_Code_Utf16LE
}else if bt[0] == 0xFE && bt[1] == 0xFF{ //UTF-16(big Endian)
file.Seek(-1,io.SeekCurrent)
filecodeType = File_Code_Utf16BE
}else if bt[0] == 0xEF && bt[1] == 0xBB && bt[2] == 0xBF { //UTF-8
filecodeType = File_Code_Utf8
}else{
file.Seek(-3,io.SeekCurrent)
}
}
reader := bufio.NewReader(file)
for{
line,err := reader.ReadBytes('\n')
if filecodeType == File_Code_Utf16LE{ //小端结尾多一个空白的0标记
reader.ReadByte()
}
if err == nil || err == io.EOF{
linelen := len(line)
if linelen >= 2{
if line[linelen-2] == '\r'{
line = line[:linelen - 2]
}else if line[linelen - 1] == '\n'{
line = line[:linelen-1]
}
}
if linelen>0{
switch filecodeType {
case File_Code_Utf8:
lst.Add(FastByte2String(line))
case File_Code_Utf16LE,File_Code_Utf16BE:
lst.Add(UTF16Byte2string(line,filecodeType == File_Code_Utf16BE))
case File_Code_GBK,File_Code_Unknown:
if tmpbytes, err := GBK2Utf8(line); err == nil {
lst.Add(FastByte2String(tmpbytes))
}else{
lst.Add(FastByte2String(line))
}
}
}else{
lst.Add("")
}
if err != nil{
return
}
}else{
return
}
}
}
}
}
func (lst *GStringList)LoadFromReader(r io.Reader,filecodeType FileCodeMode) {
lst.Clear()
reader := bufio.NewReader(r)
for{
line,err := reader.ReadBytes('\n')
if filecodeType == File_Code_Utf16LE{ //小端结尾多一个空白的0标记
reader.ReadByte()
}
if err == nil || err == io.EOF{
linelen := len(line)
if linelen >= 2{
if line[linelen-2] == '\r'{
line = line[:linelen - 2]
}else if line[linelen - 1] == '\n'{
line = line[:linelen-1]
}
}
if linelen>0{
switch filecodeType {
case File_Code_Utf8:
lst.Add(FastByte2String(line))
case File_Code_Utf16LE,File_Code_Utf16BE:
lst.Add(UTF16Byte2string(line,filecodeType == File_Code_Utf16BE))
case File_Code_GBK,File_Code_Unknown:
if tmpbytes, err := GBK2Utf8(line); err == nil {
lst.Add(FastByte2String(tmpbytes))
}else{
lst.Add(FastByte2String(line))
}
}
}else{
lst.Add("")
}
if err != nil{
return
}
}else{
return
}
}
}
func (lst *GStringList) SaveToFile(fileName string) {
//文件要先写入UTF8的BOM
if file, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC, 0644); err == nil {
count := lst.Count()
if count > 0 {
file.Write([]byte{0xEF, 0xBB, 0xBF})
//写入内容
buffer := bytes.NewBuffer(make([]byte,0,1024))
for i := 0;i<count;i++{
buffer.WriteString(lst.strings[i])
if i < count - 1{
buffer.WriteString(lst.LineBreakStr())
}
}
file.Write(buffer.Bytes())
}
file.Close()
}
}
func (lst *GStringList) Add(str string) {
if lst.strings == nil {
lst.strings = make([]string, 0, 64)
}
lst.strings = append(lst.strings, str)
}
func (lst *GStringList) Insert(Index int, str string) {
if lst.strings == nil {
lst.strings = make([]string, 0, 64)
}
if Index >= 0 && Index < len(lst.strings) {
temp := append([]string{}, lst.strings[Index:]...)
lst.strings = append(lst.strings[0:Index], str)
lst.strings = append(lst.strings, temp...)
} else {
lst.Add(str)
}
}
func (lst *GStringList) Delete(index int) {
if lst.Count() > 0 && index >= 0 && index < len(lst.strings) {
lst.strings = append(lst.strings[0:index], lst.strings[index+1:]...)
}
}
func (lst *GStringList) AddStrings(strs IStrings) {
for idx := 0; idx < strs.Count(); idx++ {
lst.Add(strs.Strings(idx))
}
}
func (lst *GStringList) AddSlice(strs []string) {
if lst.strings != nil {
lst.strings = append(lst.strings, strs...)
} else {
lst.strings = strs
}
}
func (lst *GStringList) Clear() {
if lst.strings != nil {
lst.strings = lst.strings[:0]
}
}
func (lst *GStringList) IndexOf(str string) int {
if lst.Count() > 0 {
for idx, v := range lst.strings {
if v == str {
return idx
}
}
}
return -1
}
func (lst *GStringList) AddPair(Name, Value string) {
lst.strings = append(lst.strings, fmt.Sprintf("%s=%s", Name, Value))
}
func (lst *GStringList) IndexOfName(Name string) int {
if lst.Count() > 0 {
for idx, v := range lst.strings {
if eidx := strings.IndexByte(v, '='); eidx > 0 {
bt := []byte(v)
if string(bt[:eidx]) == Name {
return idx
}
}
}
}
return -1
}
func (lst *GStringList) ValueFromIndex(index int) string {
if lst.Count() == 0 {
return ""
}
if index >= 0 && index < len(lst.strings) {
str := lst.strings[index]
if idx := strings.IndexByte(str, '='); idx > 0 {
bt := []byte(str)
return FastByte2String(bt[idx+1:])
}
return ""
}
return ""
}
func (lst *GStringList) ValueByName(Name string) string {
if lst.Count() > 0 {
for _, v := range lst.strings {
if eidx := strings.IndexByte(v, '='); eidx > 0 {
bt := []byte(v)
if string(bt[:eidx]) == Name {
return FastByte2String(bt[eidx+1:])
}
}
}
}
return ""
}
func (lst *GStringList) Names(Index int) string {
if lst.Count() == 0 {
return ""
}
if Index >= 0 && Index < len(lst.strings) {
str := lst.strings[Index]
if idx := strings.IndexByte(str, '='); idx > 0 {
bt := []byte(str)
return FastByte2String(bt[:idx])
}
return ""
}
return ""
}
func (lst *GStringList) AsSlice() []string {
return lst.strings
}