19 #include <ndb_global.h>
20 #include <BaseString.hpp>
21 #include "basestring_vsnprintf.h"
44 const size_t n = strlen(s);
45 m_chr =
new char[n + 1];
52 memcpy(m_chr, s, n + 1);
58 if (s == NULL || n == 0)
64 m_chr =
new char[n + 1];
78 const char*
const s = str.m_chr;
79 const size_t n = str.m_len;
86 char* t =
new char[n + 1];
115 size_t n = strlen(s);
116 char* t =
new char[n + 1];
135 char* t =
new char[n + 1];
157 return assign(str.m_chr, n);
166 size_t n = strlen(s);
167 char* t =
new char[m_len + n + 1];
170 memcpy(t, m_chr, m_len);
171 memcpy(t + m_len, s, n + 1);
199 for(
size_t i=0;
i<vector.size();
i++) {
201 if(i<vector.size()-1)
219 l = basestring_vsnprintf(buf,
sizeof(buf), fmt, ap) + 1;
222 char *t =
new char[l];
232 l = basestring_vsnprintf(m_chr, l, fmt, ap);
233 assert(l == (
int)strlen(m_chr));
235 m_len = strlen(m_chr);
251 l = basestring_vsnprintf(buf,
sizeof(buf), fmt, ap) + 1;
253 char *tmp =
new char[l];
260 basestring_vsnprintf(tmp, l, fmt, ap);
280 char *str = strdup(m_chr);
281 int i, start, len, num = 0;
284 (i <= len) && ( (maxSize<0) || ((
int)v.size()<=maxSize-1) );
286 if(strchr(separator.
c_str(), str[
i]) || i == len) {
287 if(maxSize < 0 || (
int)v.size() < maxSize-1)
302 p = strchr(m_chr, c);
305 return (ssize_t)(p-m_chr);
311 p = strrchr(m_chr, c);
314 return (ssize_t)(p-m_chr);
321 ssize_t len = stop-start;
325 s.
assign(m_chr+start, len);
347 char *t = strdup(argv0);
353 if (vargv.push_back(t))
360 char *tmp =
new char[strlen(src)+1];
363 for(
size_t i = 0;
i < vargv.size();
i++)
369 const char *end = src + strlen(src);
377 while(src < end && *src) {
379 while(src < end && *src && iswhite(*src))
383 while(src < end && *src) {
387 while(src < end && *src && *src !=
'"') {
400 else if(iswhite(*src))
412 char *t = strdup(begin);
416 for(
size_t i = 0;
i < vargv.size();
i++)
421 if (vargv.push_back(t))
425 for(
size_t i = 0;
i < vargv.size();
i++)
434 if (vargv.push_back(NULL))
436 for(
size_t i = 0;
i < vargv.size();
i++)
444 char **argv = (
char **)malloc(
sizeof(*argv) * (vargv.size()));
447 for(
size_t i = 0;
i < vargv.size();
i++)
453 for(
size_t i = 0;
i < vargv.size();
i++){
463 m_len = strlen(m_chr);
469 int len = strlen(str) - 1;
470 for(; len > 0 && strchr(delim, str[len]); len--)
474 for(; pos <= len && strchr(delim, str[pos]); pos++)
481 memmove(str, &str[pos], len - pos + 1);
489 BaseString::vsnprintf(
char *str,
size_t size,
const char *format, va_list ap)
491 return(basestring_vsnprintf(str, size, format, ap));
498 va_start(ap, format);
499 int ret= basestring_vsnprintf(str, size, format, ap);
505 BaseString::getText(
unsigned size,
const Uint32 data[])
508 char *
buf = (
char*)malloc(32*size+1);
519 BaseString::getPrettyText(
unsigned size,
const Uint32 data[])
521 const char* delimiter =
"";
523 const unsigned MAX_BITS =
sizeof(Uint32) * 8 * size;
525 for (
unsigned i = 0;
i < MAX_BITS;
i++)
529 to.
appfmt(
"%s%d", delimiter,
i);
541 BaseString::getPrettyTextShort(
unsigned size,
const Uint32 data[])
543 const char* delimiter =
"";
544 const unsigned MAX_BITS =
sizeof(Uint32) * 8 * size;
546 for (
unsigned i = 0;
i < MAX_BITS;
i++)
550 to.
appfmt(
"%s%d", delimiter,
i);
558 BaseString_get_key(
const void* key,
size_t* key_length)
561 *key_length = str->
length();
565 #ifdef TEST_BASE_STRING
567 #include <NdbTap.hpp>
579 for (
unsigned i = 0;
i < 1000;
i++) {
582 OK(strlen(t.c_str()) % 3 == 0);
588 OK(s.split(v,
":;") == 7);
602 OK(s.split(v,
":;", 4) == 4);
607 OK(v[3] ==
"foo:bar");
611 OK(n ==
"()123()abc()foo:bar");
614 OK(n ==
" 123 abc foo:bar");
618 OK(
BaseString(
"hamburger").substr(4,2) ==
"");
619 OK(
BaseString(
"hamburger").substr(3) ==
"burger");
620 OK(
BaseString(
"hamburger").substr(4,8) ==
"urge");
621 OK(
BaseString(
"smiles").substr(1,5) ==
"mile");
630 OK(
BaseString(
"abc\t\n\r kalleabc\t\r\n").trim(
"abc\t\r\n ") ==
"kalle");
639 OK(s3.
append((
const char*)NULL) ==
"");
640 OK(s4.append((
const char*)NULL) ==
"elf");
641 OK(s4.append(s3) ==
"elf");
642 OK(s4.append(s2) ==
"elf");
643 OK(s4.append(s4) ==
"elfelf");
646 OK(s4.assign((
const char*)NULL).c_str() == NULL);
647 OK(s4.assign(s4).c_str() == NULL);
650 for(
int i=0;
i<20;
i++)
652 #define BIG_ASSFMT_OK(X) do{u_int x=(X);OK(s2.assfmt("%*s",x,"Z").length() == x);}while(0)
660 BIG_ASSFMT_OK(20*1024*1024);