DOHString *DohNewStringWithSize(const DOHString_or_char *so, int len) { int l = 0, max; String *str; char *s; if (DohCheck(so)) { s = (char *) String_data((String *) so); } else { s = (char *) so; } str = (String *) DohMalloc(sizeof(String)); str->hashkey = -1; str->sp = 0; str->line = 1; str->file = 0; max = INIT_MAXSIZE; if (s) { l = (int) len; if ((l + 1) > max) max = l + 1; } str->str = (char *) DohMalloc(max); str->maxsize = max; if (s) { strncpy(str->str, s, len); str->len = l; str->sp = l; } else { str->str[0] = 0; str->len = 0; } return DohObjMalloc(&DohStringType, str); }
static int String_insert(DOH *so, int pos, DOH *str) { String *s; char *nstr; int len; char *data; if (pos == DOH_END) { DohString_append(so, str); return 0; } s = (String *) ObjData(so); s->hashkey = -1; if (DohCheck(str)) { String *ss = (String *) ObjData(str); data = (char *) String_data(str); len = ss->len; } else { data = (char *) (str); len = (int) strlen(data); } nstr = s->str; if (pos < 0) pos = 0; else if (pos > s->len) pos = s->len; /* See if there is room to insert the new data */ while (s->maxsize <= s->len + len) { int newsize = 2 * s->maxsize; s->str = (char *) DohRealloc(s->str, newsize); assert(s->str); s->maxsize = newsize; } memmove(s->str + pos + len, s->str + pos, (s->len - pos)); memcpy(s->str + pos, data, len); if (s->sp >= pos) { int i; for (i = 0; i < len; i++) { if (data[i] == '\n') s->line++; } s->sp += len; } s->len += len; s->str[s->len] = 0; return 0; }
static void DohString_append(DOH *so, const DOHString_or_char *str) { int oldlen, newlen, newmaxsize, l, sp; char *tc; String *s = (String *) ObjData(so); char *newstr = 0; if (DohCheck(str)) { String *ss = (String *) ObjData(str); newstr = (char *) String_data((DOH *) str); l = ss->len; } else { newstr = (char *) (str); l = (int) strlen(newstr); } if (!newstr) return; s->hashkey = -1; oldlen = s->len; newlen = oldlen + l + 1; if (newlen >= s->maxsize - 1) { newmaxsize = 2 * s->maxsize; if (newlen >= newmaxsize - 1) newmaxsize = newlen + 1; s->str = (char *) DohRealloc(s->str, newmaxsize); assert(s->str); s->maxsize = newmaxsize; } tc = s->str; memcpy(tc + oldlen, newstr, l + 1); sp = s->sp; if (sp >= oldlen) { int i = oldlen + l - sp; tc += sp; for (; i; --i) { if (*(tc++) == '\n') s->line++; } s->sp = oldlen + l; } s->len += l; }
DOHString *DohNewString(const DOHString_or_char *so) { int l = 0, max; String *str; char *s; int hashkey = -1; if (DohCheck(so)) { str = (String *) ObjData(so); s = (char *) String_data((String *) so); l = s ? str->len : 0; hashkey = str->hashkey; } else { s = (char *) so; l = s ? (int) strlen(s) : 0; } str = (String *) DohMalloc(sizeof(String)); str->hashkey = hashkey; str->sp = 0; str->line = 1; str->file = 0; max = INIT_MAXSIZE; if (s) { if ((l + 1) > max) max = l + 1; } str->str = (char *) DohMalloc(max); str->maxsize = max; if (s) { strcpy(str->str, s); str->len = l; str->sp = l; } else { str->str[0] = 0; str->len = 0; } return DohObjMalloc(&DohStringType, str); }