1 //========================================================================
5 // Simple variable-length string type.
7 // Copyright 1996 Derek B. Noonburg
9 //========================================================================
23 // Create an empty string.
26 // Create a string from a C string.
27 GString(const char *s1);
29 // Create a string from <length1> chars at <s1>. This string
30 // can contain null characters.
31 GString (const char *s1, int length1);
34 GString(GString *str);
35 GString *copy() { return new GString(this); }
37 // Concatenate two strings.
38 GString(GString *str1, GString *str2);
40 // Convert an integer to a string.
41 static GString *fromInt(int x);
47 int getLength() { return length; }
50 char *getCString() { return s; }
52 // Get <i>th character.
53 char getChar(int i) { return s[i]; }
55 // Change <i>th character.
56 void setChar(int i, char c) { s[i] = c; }
58 // Clear string to zero length.
61 // Append a character or string.
62 GString *append(char c);
63 GString *append(GString *str);
64 GString *append(const char *str);
65 GString *append(const char *str, int length1);
67 // Insert a character or string.
68 GString *insert(int i, char c);
69 GString *insert(int i, GString *str);
70 GString *insert(int i, const char *str);
71 GString *insert(int i, const char *str, int length1);
73 // Delete a character or range of characters.
74 GString *del(int i, int n = 1);
76 // Convert string to all-upper/all-lower case.
80 // Compare two strings: -1:< 0:= +1:>
81 // These functions assume the strings do not contain null characters.
82 int cmp(GString *str) { return strcmp(s, str->getCString()); }
83 int cmpN(GString *str, int n) { return strncmp(s, str->getCString(), n); }
84 int cmp(const char *s1) { return strcmp(s, s1); }
85 int cmpN(const char *s1, int n) { return strncmp(s, s1, n); }
92 void resize(int length1);