-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkstring.h
238 lines (221 loc) · 6.3 KB
/
kstring.h
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
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <[email protected]>
Ported from htslib/kstring.h to be more C++ friendly
The MIT License
Copyright (C) 2011 by Attractive Chaos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef PHENIQS_KSTRING_H
#define PHENIQS_KSTRING_H
#include "include.h"
#include "error.h"
#define tag_to_code(t) static_cast< uint16_t >(*(t)) << 8 | static_cast< uint8_t >(*((t) + 1))
const char LINE_BREAK('\n');
static inline void ks_increase_by_size(kstring_t& s, size_t size) {
size += s.l;
if(size > s.m) {
kroundup_size_t(size);
char* temp;
if((temp = static_cast< char* >(realloc(s.s, size))) == NULL) {
throw OutOfMemoryError();
}
s.s = temp;
s.m = size;
}
};
static inline void ks_increase_to_size(kstring_t& s, size_t size) {
if(s.m < size) {
kroundup_size_t(size);
char* temp;
if((temp = static_cast< char* >(realloc(s.s, size))) == NULL) {
throw OutOfMemoryError();
}
s.s = temp;
s.m = size;
}
};
static inline bool ks_empty(const kstring_t& s) {
return s.l == 0;
};
static inline bool ks_not_empty(const kstring_t& s) {
return s.l > 0;
};
static inline void ks_clear(kstring_t& s) {
s.l = 0;
if(s.s != NULL) {
s.s[0] = '\0';
}
};
static inline void ks_free(kstring_t& s) {
if(s.s != NULL) {
free(s.s);
s.s = NULL;
}
s.l = 0;
s.m = 0;
};
static inline void ks_terminate(kstring_t& s) {
ks_increase_by_size(s, 1);
s.s[s.l] = '\0';
};
static inline void ks_put_character(const char c, kstring_t& s) {
ks_increase_by_size(s, 2);
s.s[s.l] = c;
++s.l;
s.s[s.l] = '\0';
};
static inline void ks_put_character_(const char c, kstring_t& s) {
ks_increase_by_size(s, 1);
s.s[s.l] = c;
++s.l;
};
static inline void ks_put_string(const char* p, size_t l, kstring_t& s) {
ks_increase_by_size(s, l + 2);
memcpy(s.s + s.l, p, l);
s.l += l;
s.s[s.l] = '\0';
};
static inline void ks_put_string(const kstring_t& from, kstring_t& to) {
ks_increase_by_size(to, from.l + 2);
memcpy(to.s + to.l, from.s, from.l);
to.l += from.l;
to.s[to.l] = '\0';
};
static inline void ks_put_string(const char* p, kstring_t& s) {
return ks_put_string(p, strlen(p), s);
};
static inline void ks_put_string(const string& from, kstring_t& to) {
ks_increase_by_size(to, from.size() + 2);
memcpy(to.s + to.l, from.c_str(), from.size());
to.l += from.size();
to.s[to.l] = '\0';
};
static inline void ks_put_string_(const string& from, kstring_t& to) {
ks_increase_by_size(to, from.size() + 2);
memcpy(to.s + to.l, from.c_str(), from.size());
to.l += from.size();
};
static inline void ks_put_string_(const void* p, size_t l, kstring_t& s) {
ks_increase_by_size(s, l);
memcpy(s.s + s.l, p, l);
s.l += l;
};
static inline void ks_put_string_(const kstring_t& from, kstring_t& to) {
ks_increase_by_size(to, from.l);
memcpy(to.s + to.l, from.s, from.l);
to.l += from.l;
};
static inline void ks_put_uint16(const uint16_t& c, kstring_t& s) {
char buffer[8];
uint16_t x;
int16_t l(0);
int16_t i(0);
if(c == 0) {
ks_put_character('0', s);
} else {
for(l = 0, x = c; x > 0; x /= 10) {
buffer[l] = x % 10 + '0';
++l;
}
ks_increase_by_size(s, l + 2);
for(i = l - 1; i >= 0; --i) {
s.s[s.l] = buffer[i];
++s.l;
}
s.s[s.l] = '\0';
}
};
static inline void ks_put_int32(const int32_t& c, kstring_t& s) {
char buffer[16];
uint32_t x(c);
int16_t i(0);
int16_t l(0);
if(c < 0) {
x = -x;
}
do {
buffer[l] = x % 10 + '0';
++l;
x /= 10;
} while(x > 0);
if(c < 0) {
buffer[l] = '-';
++l;
}
ks_increase_by_size(s, l + 2);
for(i = l - 1; i >= 0; --i) {
s.s[s.l] = buffer[i];
++s.l;
}
s.s[s.l] = '\0';
};
static inline void ks_put_uint32(const uint32_t c, kstring_t& s) {
char buffer[16];
uint32_t x;
int16_t l(0);
int16_t i(0);
if(c == 0) {
ks_put_character('0', s);
} else {
for(l = 0, x = c; x > 0; x /= 10) {
buffer[l] = x % 10 + '0';
++l;
}
ks_increase_by_size(s, l + 2);
for(i = l - 1; i >= 0; --i) {
s.s[s.l] = buffer[i];
++s.l;
}
s.s[s.l] = '\0';
}
};
static inline void ks_put_int64(const int64_t c, kstring_t& s) {
char buffer[32];
int64_t x(c);
int i(0);
int l(0);
if(c < 0) {
x = -x;
}
do {
buffer[l] = x % 10 + '0';
++l;
x /= 10;
} while(x > 0);
if(c < 0) {
buffer[l] = '-';
++l;
}
ks_increase_by_size(s, l + 2);
for(i = l - 1; i >= 0; --i) {
s.s[s.l] = buffer[i];
++s.l;
}
s.s[s.l] = '\0';
};
static inline void ks_assign_string(const kstring_t& from, kstring_t& to) {
ks_increase_to_size(to, from.l + 2);
memcpy(to.s, from.s, from.l);
to.l = from.l;
to.s[to.l] = '\0';
};
#endif /* PHENIQS_KSTRING_H */