-
Notifications
You must be signed in to change notification settings - Fork 3
/
ListBuffer.cc
113 lines (91 loc) · 1.93 KB
/
ListBuffer.cc
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
//
// Copyright 2009-2017 M. Shulhan (ms@kilabit.info). All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "ListBuffer.hh"
namespace vos {
void _list_buffer_add(List* buffers, const char* v, size_t start, size_t end
, int trim)
{
Buffer* b = NULL;
size_t len = end - start;
if (len == 0) {
if (trim) {
return;
}
b = new Buffer(1);
} else {
b = new Buffer(len);
b->copy_raw(&v[start], len);
if (trim) {
b->trim();
if (b->is_empty()) {
delete b;
return;
}
}
}
buffers->push_tail(b);
}
/**
* Method `split_by_char(b, sep, trim)` will split buffer `b` using separator
* `sep` and return list of buffers.
*
* If buffer `b` is empty it will return NULL.
*
* If `trim` is non zero value, splitted buffer will be trimmed, and if result
* of trimmed buffer is empty then it would not be added to the list.
*/
List* SPLIT_BY_CHAR(Buffer* b, const char sep, int trim)
{
if (!b) {
return NULL;
}
if (b->is_empty()) {
return NULL;
}
List* buffers = new List();
size_t x = 0;
size_t start = 0;
for (; x < b->len(); x++) {
if (b->char_at(x) == sep) {
_list_buffer_add(buffers, b->v(), start, x, trim);
start = x + 1;
}
}
if (x >= start) {
_list_buffer_add(buffers, b->v(), start, x, trim);
}
return buffers;
}
/**
* Method `split_by_whitespace(Buffer* b)` will split buffer using whitespace
* and return list of buffers.
*/
List* SPLIT_BY_WHITESPACE(Buffer* b)
{
if (!b) {
return NULL;
}
b->trim();
// skip empty line.
if (b->is_empty()) {
return NULL;
}
List* buffers = new List();
size_t x = 0;
size_t start = 0;
for (; x < b->len(); x++) {
if (isspace(b->char_at(x))) {
_list_buffer_add(buffers, b->v(), start, x, 1);
start = x + 1;
}
}
if (x > start) {
_list_buffer_add(buffers, b->v(), start, x, 1);
}
return buffers;
}
} // namespace::vos
// vi: ts=8 sw=8 tw=80: