Changeset 7944 for trunk/mgpp


Ignore:
Timestamp:
2004-08-14T07:30:16+12:00 (20 years ago)
Author:
kjdon
Message:

trying to keep mgpp up to date with gsdl version. added in mdewsnip changes 2004/05/24: (Human Info) Added a variant of SetCStr that allows you to specify an initial size for the string (for more efficient memory usage). (Human Info) Changed iterators from here++ to ++here, and allocated memory for arrays a little better.

Location:
trunk/mgpp/text
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/mgpp/text/UCArray.cpp

    r3365 r7944  
    3030    text.push_back (*cStr);
    3131    cStr++;
     32  }
     33}
     34
     35void SetCStr (UCArray &text, const char *cStr, size_t nSizeHint) {
     36  text.erase(text.begin(), text.end());
     37
     38  // reserve the needed space in advance
     39  if (text.capacity() < nSizeHint + 1) {
     40    text.reserve(nSizeHint + 1);
     41  }
     42  while (*cStr != '\0') {
     43    text.push_back (*cStr);
     44    ++cStr;
    3245  }
    3346}
     
    5366  while (here != end) {
    5467    s << *here;
    55     here++;
     68    ++here;
    5669  }
    5770
     
    133146  if (!ReadVarLenUL (f, arraySize)) return false;
    134147
     148  // reserve the needed space in advance
     149  if (a.capacity() < arraySize + 1) {
     150    a.reserve(arraySize + 1);
     151  }
     152
    135153  // read in the array
    136154  unsigned char b = 0;
     
    156174    if (ferror (f) != 0) return false;
    157175   
    158     here++;
     176    ++here;
    159177  }
    160178 
     
    219237      pos = diff;
    220238
    221     a1Here++;
    222     a2Here++;
     239    ++a1Here;
     240    ++a2Here;
    223241  }
    224242
     
    234252
    235253  while (i < l && *a1Here == *a2Here) {
    236     i++; a1Here++; a2Here++;
     254    i++; ++a1Here; ++a2Here;
    237255  }
    238256 
     
    255273  char* tmp=GetCStr(a);
    256274  int ret=(fwrite (tmp+preLen, sizeof (char), sufLen, f) == sufLen);
    257   delete (tmp);
    258   return (ret);
     275  delete []tmp;
     276  return (ret != 0);
    259277}
    260278
     
    267285  sufLen = fgetc(f);
    268286
    269   if (a.size () > preLen) a.erase (a.begin()+preLen, a.end());
     287  if (a.size() > preLen) a.erase (a.begin()+preLen, a.end());
     288
     289  // reserve the needed space in advance
     290  if (a.capacity() < a.size() + sufLen + 1) {
     291    a.reserve(a.size() + sufLen + 1);
     292  }
     293
    270294  while (sufLen > 0) {
    271295    unsigned char c = fgetc (f);
  • trunk/mgpp/text/UCArray.h

    r3365 r7944  
    5555// functions to manipulate UCArrays
    5656void SetCStr (UCArray &text, const char *cStr);
     57// same as SetCStr but first tries to allocate nSizeHint space (only if needed)
     58void SetCStr (UCArray &text, const char *cStr, size_t nSizeHint);
    5759char * GetCStr(UCArray text);
    5860inline void UCArrayClear (UCArray &a) {
Note: See TracChangeset for help on using the changeset viewer.