GooString.h 3.03 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//========================================================================
//
// GooString.h
//
// Simple variable-length string type.
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

#ifndef GSTRING_H
#define GSTRING_H

#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif

Albert Astals Cid's avatar
Albert Astals Cid committed
18
#include <stdlib.h> // for NULL
19 20
#include "gtypes.h"

Kristian Høgsberg's avatar
Kristian Høgsberg committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class GooString {
public:

  // Create an empty string.
  GooString();

  // Create a string from a C string.
  GooString(const char *sA);

  // Create a string from <lengthA> chars at <sA>.  This string
  // can contain null characters.
  GooString(const char *sA, int lengthA);

  // Create a string from <lengthA> chars at <idx> in <str>.
  GooString(GooString *str, int idx, int lengthA);

Albert Astals Cid's avatar
Albert Astals Cid committed
37 38 39 40 41 42
  // Set content of a string to concatination of <s1> and <s2>. They can both
  // be NULL. if <s1Len> or <s2Len> is CALC_STRING_LEN, then length of the string
  // will be calculated with strlen(). Otherwise we assume they are a valid
  // length of string (or its substring)
  GooString* Set(const char *s1, int s1Len=CALC_STRING_LEN, const char *s2=NULL, int s2Len=CALC_STRING_LEN);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  // Copy a string.
  GooString(GooString *str);
  GooString *copy() { return new GooString(this); }

  // Concatenate two strings.
  GooString(GooString *str1, GooString *str2);

  // Convert an integer to a string.
  static GooString *fromInt(int x);

  // Destructor.
  ~GooString();

  // Get length.
  int getLength() { return length; }

  // Get C string.
  char *getCString() { return s; }

  // Get <i>th character.
  char getChar(int i) { return s[i]; }

  // Change <i>th character.
  void setChar(int i, char c) { s[i] = c; }

  // Clear string to zero length.
  GooString *clear();

  // Append a character or string.
  GooString *append(char c);
  GooString *append(GooString *str);
Albert Astals Cid's avatar
Albert Astals Cid committed
74
  GooString *append(const char *str, int lengthA=CALC_STRING_LEN);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
75 76 77 78

  // Insert a character or string.
  GooString *insert(int i, char c);
  GooString *insert(int i, GooString *str);
Albert Astals Cid's avatar
Albert Astals Cid committed
79
  GooString *insert(int i, const char *str, int lengthA=CALC_STRING_LEN);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
80 81 82 83 84 85 86 87 88

  // Delete a character or range of characters.
  GooString *del(int i, int n = 1);

  // Convert string to all-upper/all-lower case.
  GooString *upperCase();
  GooString *lowerCase();

  // Compare two strings:  -1:<  0:=  +1:>
89 90 91 92
  int cmp(GooString *str);
  int cmpN(GooString *str, int n);
  int cmp(const char *sA);
  int cmpN(const char *sA, int n);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
93

94 95
  GBool hasUnicodeMarker(void);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
96
private:
Albert Astals Cid's avatar
Albert Astals Cid committed
97 98 99 100 101 102 103 104 105 106 107 108
  // you can tweak this number for a different speed/memory usage tradeoffs.
  // In libc malloc() rounding is 16 so it's best to choose a value that
  // results in sizeof(GooString) be a multiple of 16.
  // 24 makes sizeof(GooString) to be 32.
  static const int STR_STATIC_SIZE = 24;
  // a special value telling that the length of the string is not given
  // so it must be calculated from the strings
  static const int CALC_STRING_LEN = -1;

  int  roundedSize(int len);

  char sStatic[STR_STATIC_SIZE];
Kristian Høgsberg's avatar
Kristian Høgsberg committed
109 110 111
  int length;
  char *s;

Albert Astals Cid's avatar
Albert Astals Cid committed
112
  void resize(int newLength);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
113 114 115
};

#endif