| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// GString.h |
| 4 |
// |
| 5 |
// Simple variable-length string type. |
| 6 |
// |
| 7 |
// Copyright 1996-2003 Glyph & Cog, LLC |
| 8 |
// |
| 9 |
//======================================================================== |
| 10 |
|
| 11 |
#ifndef GSTRING_H |
| 12 |
#define GSTRING_H |
| 13 |
|
| 14 |
#include <aconf.h> |
| 15 |
|
| 16 |
#include <limits.h> // for LLONG_MAX and ULLONG_MAX |
| 17 |
#include <stdarg.h> |
| 18 |
#include "gtypes.h" |
| 19 |
|
| 20 |
class GString { |
| 21 |
public: |
| 22 |
|
| 23 |
// Create an empty string. |
| 24 |
GString(); |
| 25 |
|
| 26 |
// Create a string from a C string. |
| 27 |
GString(const char *sA); |
| 28 |
|
| 29 |
// Create a string from <lengthA> chars at <sA>. This string |
| 30 |
// can contain null characters. |
| 31 |
GString(const char *sA, int lengthA); |
| 32 |
|
| 33 |
// Create a string from <lengthA> chars at <idx> in <str>. |
| 34 |
GString(GString *str, int idx, int lengthA); |
| 35 |
|
| 36 |
// Copy a string. |
| 37 |
GString(GString *str); |
| 38 |
GString *copy() { return new GString(this); } |
| 39 |
|
| 40 |
// Concatenate two strings. |
| 41 |
GString(GString *str1, GString *str2); |
| 42 |
|
| 43 |
// Convert an integer to a string. |
| 44 |
static GString *fromInt(int x); |
| 45 |
|
| 46 |
// Create a formatted string. Similar to printf, but without the |
| 47 |
// string overflow issues. Formatting elements consist of: |
| 48 |
// {<arg>:[<width>][.<precision>]<type>} |
| 49 |
// where: |
| 50 |
// - <arg> is the argument number (arg 0 is the first argument |
| 51 |
// following the format string) -- NB: args must be first used in |
| 52 |
// order; they can be reused in any order |
| 53 |
// - <width> is the field width -- negative to reverse the alignment; |
| 54 |
// starting with a leading zero to zero-fill (for integers) |
| 55 |
// - <precision> is the number of digits to the right of the decimal |
| 56 |
// point (for floating point numbers) |
| 57 |
// - <type> is one of: |
| 58 |
// d, x, o, b -- int in decimal, hex, octal, binary |
| 59 |
// ud, ux, uo, ub -- unsigned int |
| 60 |
// ld, lx, lo, lb, uld, ulx, ulo, ulb -- long, unsigned long |
| 61 |
// lld, llx, llo, llb, ulld, ullx, ullo, ullb |
| 62 |
// -- long long, unsigned long long |
| 63 |
// f, g -- double |
| 64 |
// c -- char |
| 65 |
// s -- string (char *) |
| 66 |
// t -- GString * |
| 67 |
// w -- blank space; arg determines width |
| 68 |
// To get literal curly braces, use {{ or }}. |
| 69 |
static GString *format(const char *fmt, ...); |
| 70 |
static GString *formatv(const char *fmt, va_list argList); |
| 71 |
|
| 72 |
// Destructor. |
| 73 |
~GString(); |
| 74 |
|
| 75 |
// Get length. |
| 76 |
int getLength() { return length; } |
| 77 |
|
| 78 |
// Get C string. |
| 79 |
char *getCString() { return s; } |
| 80 |
|
| 81 |
// Get <i>th character. |
| 82 |
char getChar(int i) { return s[i]; } |
| 83 |
|
| 84 |
// Change <i>th character. |
| 85 |
void setChar(int i, char c) { s[i] = c; } |
| 86 |
|
| 87 |
// Clear string to zero length. |
| 88 |
GString *clear(); |
| 89 |
|
| 90 |
// Append a character or string. |
| 91 |
GString *append(char c); |
| 92 |
GString *append(GString *str); |
| 93 |
GString *append(const char *str); |
| 94 |
GString *append(const char *str, int lengthA); |
| 95 |
|
| 96 |
// Append a formatted string. |
| 97 |
GString *appendf(const char *fmt, ...); |
| 98 |
GString *appendfv(const char *fmt, va_list argList); |
| 99 |
|
| 100 |
// Insert a character or string. |
| 101 |
GString *insert(int i, char c); |
| 102 |
GString *insert(int i, GString *str); |
| 103 |
GString *insert(int i, const char *str); |
| 104 |
GString *insert(int i, const char *str, int lengthA); |
| 105 |
|
| 106 |
// Delete a character or range of characters. |
| 107 |
GString *del(int i, int n = 1); |
| 108 |
|
| 109 |
// Convert string to all-upper/all-lower case. |
| 110 |
GString *upperCase(); |
| 111 |
GString *lowerCase(); |
| 112 |
|
| 113 |
// Compare two strings: -1:< 0:= +1:> |
| 114 |
int cmp(GString *str); |
| 115 |
int cmpN(GString *str, int n); |
| 116 |
int cmp(const char *sA); |
| 117 |
int cmpN(const char *sA, int n); |
| 118 |
|
| 119 |
private: |
| 120 |
|
| 121 |
int length; |
| 122 |
char *s; |
| 123 |
|
| 124 |
void resize(int length1); |
| 125 |
#ifdef LLONG_MAX |
| 126 |
static void formatInt(long long x, char *buf, int bufSize, |
| 127 |
GBool zeroFill, int width, int base, |
| 128 |
const char **p, int *len); |
| 129 |
#else |
| 130 |
static void formatInt(long x, char *buf, int bufSize, |
| 131 |
GBool zeroFill, int width, int base, |
| 132 |
const char **p, int *len); |
| 133 |
#endif |
| 134 |
#ifdef ULLONG_MAX |
| 135 |
static void formatUInt(unsigned long long x, char *buf, int bufSize, |
| 136 |
GBool zeroFill, int width, int base, |
| 137 |
const char **p, int *len); |
| 138 |
#else |
| 139 |
static void formatUInt(Gulong x, char *buf, int bufSize, |
| 140 |
GBool zeroFill, int width, int base, |
| 141 |
const char **p, int *len); |
| 142 |
#endif |
| 143 |
static void formatDouble(double x, char *buf, int bufSize, int prec, |
| 144 |
GBool trim, const char **p, int *len); |
| 145 |
}; |
| 146 |
|
| 147 |
#endif |