/* * Implementation of str.h. It's not pretty, particularly the hashing. */ #include #include #include #include #include "str.h" /** * Implementations of public functions. */ String* NewString() { String* newstr; newstr = (String *) malloc(sizeof(struct StringStruct)); newstr->len = 0; newstr->data = null; return newstr; } String* NewString1(char* s) { String* newstr = NewString(); newstr->len = strlen(s); newstr->data = strcpy((char *)malloc(newstr->len + 1), s); return newstr; } String* NewStringQuotes(char* s) { String* newstr = NewString(); int len = strlen(s); if (len >= 2) { newstr->len = len - 2; newstr->data = strncpy((char *)malloc(newstr->len + 1), &(s[1]), newstr->len); } /* Null terminate manually, since s's null terminator was not copied. */ newstr->data[newstr->len] = '\0'; return newstr; } void DelString(String* s) { /* Unimplemented at present. */ } int StringLen(String* s) { if (s) return s->len; else return 0; } int StringCompare(String* s1, String* s2) { if (s1 == s2) return 0; if ((not s1) and (not s2)) return 0; if (not s1) return -1; if (not s2) return 1; if (s1->data == s2->data) return 0; return strcmp(s1->data, s2->data); } int StringCompareCS(String* s1, char* s2) { if ((not s1) and (not s2)) return 0; if (not s1) return -1; if (not s2) return 1; return strcmp(s1->data, s2); } bool StringEqual(String* s1, String* s2) { if ((not s1) and (not s2)) return true; if (not s1) return false; if (not s2) return false; return StringCompare(s1, s2) == 0; } bool StringEqualCS(String* s1, char* s2) { return not StringCompareCS(s1, s2); } bool StringContains(String* s1, String* s2) { if (not s1) return false; if (not s2) return false; return (strstr(s1->data, s2->data) != null); } String* StringConcat(String* s1, String* s2) { String* newstr; if ((not s1) and (not s2)) return NewString(""); if ((not s1) or (s1->data == null)) return StringCopy(s2); if ((not s2) or (s2->data == null)) return StringCopy(s1); newstr = NewString(); newstr->len = s1->len + s2->len; newstr->data = strcpy((char*)malloc(newstr->len + 1), s1->data); strcat(newstr->data, s2->data); return newstr; } String* StringConcatCS(String* s1, char* s2) { String* newstr; char* newcs; if ((not s1) and (not s2)) return NewString(""); if (not s1) return NewString(s2); if (not s2) return StringCopy(s1); newstr = NewString(); newstr->len = s1->len + strlen(s2); newstr->data = strcpy((char*)malloc(newstr->len + 1), s1->data); strcat(newstr->data, s2); return newstr; } String* StringCopy(String* s) { if (s == null) { return NewString(""); } return NewString(s->data); } String* SubString(String* s, int startpos, int endpos) { String* newstr; if ((startpos < 1) or (endpos > s->len) or (startpos > endpos)) return NewString(""); newstr = NewString(); newstr->len = endpos - startpos + 1;; newstr->data = strncpy((char*)malloc(newstr->len + 1), &(s->data[startpos - 1]), newstr->len); newstr->data[newstr->len] = '\0'; return newstr; } char GetNthStringChar(String* s, int n) { if ((not s) or (n > s->len) or (n < 1)) return '\0'; return s->data[n - 1]; } String* GetNthStringCharAsString(String* s, int n) { String* newstr; newstr = NewString(); if ((not s) or (n > s->len) or (n < 1)) { return newstr; } newstr->data = strncpy((char *)malloc(2), &(s->data[n - 1]), 1); newstr->data[1] = '\0'; return newstr; } String* Stringify(int i) { String* newstr; char* newcs; newcs = (char*)malloc((int)log10((double)i) + 2); sprintf(newcs, "%d", i); newstr = NewString(newcs); free(newcs); return newstr; } char* StringConvert(String* s) { char* newstr; if (not s) { newstr = (char*)malloc(1); newstr[0] = '\0'; return newstr; } return strcpy((char*)malloc(s->len + 1), s->data); } const char* ConstStringConvert(String* s) { return s->data; }