[Avida-SVN] r2460 - in development/source: script tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Mar 17 14:20:07 PDT 2008


Author: brysonda
Date: 2008-03-17 17:20:07 -0400 (Mon, 17 Mar 2008)
New Revision: 2460

Modified:
   development/source/script/cParser.cc
   development/source/tools/cString.cc
   development/source/tools/cString.h
Log:
AS:
Add escape sequence parsing for string literals (via a new method in cString).

Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc	2008-03-17 19:57:20 UTC (rev 2459)
+++ development/source/script/cParser.cc	2008-03-17 21:20:07 UTC (rev 2460)
@@ -599,7 +599,8 @@
       expr.Set(new cASTLiteral(FILEPOS, AS_TYPE_BOOL, currentText()));
       break;
     case TOKEN(STRING):
-      expr.Set(new cASTLiteral(FILEPOS, AS_TYPE_STRING, currentText().Substring(1, currentText().GetSize() - 2)));
+      expr.Set(new cASTLiteral(FILEPOS, AS_TYPE_STRING,
+                               currentText().Substring(1, currentText().GetSize() - 2).ParseEscapeSequences()));
       break;
     case TOKEN(ID):
       if (peekToken() == TOKEN(PREC_OPEN)) {

Modified: development/source/tools/cString.cc
===================================================================
--- development/source/tools/cString.cc	2008-03-17 19:57:20 UTC (rev 2459)
+++ development/source/tools/cString.cc	2008-03-17 21:20:07 UTC (rev 2460)
@@ -81,13 +81,13 @@
   assert(in!=NULL);  // NULL input string
   int i;
   for( i = 0;
-       i < GetSize() && in[i]!='\0' && (*this)[i] == in[i];
-       i++ );
-
+      i < GetSize() && in[i]!='\0' && (*this)[i] == in[i];
+      i++ );
+  
   if( i == GetSize()  &&  in[i] == '\0' ) {
     return 0;                // *this == in
   }
-
+  
   // They're not equal!
   if ( i < GetSize() && (*this)[i] > in[i] )  return 1;  // *this > in
   return -1;                                             // *this < in
@@ -98,11 +98,11 @@
   bool found = false;
   bool goodstufffound = false;
   int j = GetSize() - 1;
-
+  
   // Scan the line from the end.  If the last non-whitespace line is
   // continueation character the line expects a line to be concatenated on
   // to it
-
+  
   while ((j >= 0) && !found && !goodstufffound) {
     if (!IsWhitespace(j)) {
       if ((*this)[j] == CONTINUE_LINE_CHAR) {
@@ -181,7 +181,7 @@
   assert (pos <= GetSize()); // Position Past End of String: setting to end.
   if (pos <= 0) pos = 0;
   else if (pos > GetSize()) pos = GetSize();
-
+  
   while( pos < GetSize() ) {
     if( (*this)[pos] == in_char) return pos; // Found!
     pos++;
@@ -196,24 +196,24 @@
   assert (pos <= GetSize()); // Position Past End of String: setting to end.
   if (pos <= 0) pos = 0;
   else if (pos > GetSize()) pos = GetSize();
-
+  
   // While there is enough space to find
   while (pos != -1 && pos + in.GetSize() < GetSize()) {
     cerr << in << " " << pos << endl;
     if( (pos=Find(in, pos)) >= 0 ){      // try to find it
       // if it's got whitespace on both sides, it's a word
       if( ( pos==0 || IsWhitespace(pos-1) )
-	  && ( pos==GetSize()-1 || IsWhitespace(pos+in.GetSize()) ) ){
-	return pos;
+         && ( pos==GetSize()-1 || IsWhitespace(pos+in.GetSize()) ) ){
+        return pos;
       } else {
-	pos++; // go on and look further down
+        pos++; // go on and look further down
       }
     }
   }
   return -1;
 }
-	
 
+
 cString cString::GetWord(int word_id) const
 {
   // Find positon of word
@@ -303,7 +303,7 @@
   assert(size > 0); // Non-Positive Size
   assert(start >= 0); // Negative Position
   assert(start + size <= GetSize()); // Position+Size Past End of String
-
+  
   cString new_string(size);
   for (int i=0; i<size; i++) {
     new_string[i] = (*this)[i+start];
@@ -314,15 +314,15 @@
 bool cString::IsSubstring(const cString & in_string, int start) const
 {
   assert (start >= 0); // Negative start position
-
+  
   // If the potential sub-string won't fit, return false;
   if ( start + in_string.GetSize() > GetSize() ) return false;
-
+  
   // Otherwise, check character by character.
   for (int i = 0; i < in_string.GetSize(); i++) {
     if ( (*this)[i+start] != in_string[i] ) return false;
   }
-
+  
   return true;
 }
 
@@ -375,7 +375,7 @@
   // Trim front
   int ws_count = CountWhitespace();
   if (ws_count > 0) InsertStr(0, NULL, 0, ws_count);
-
+  
   // Trim trailing
   ws_count = 0;
   while (GetSize() - ws_count - 1 > 0 && IsWhitespace(GetSize() - ws_count - 1)) ws_count++;
@@ -394,7 +394,7 @@
     // Trim off the front
     InsertStr(0, NULL, 0, pos+1);
   }
-
+  
   // If the deliminator is *not* found, return the whole string.
   else {
     rv = *this;
@@ -408,19 +408,19 @@
 {
   // If there is nothing here, there is nothing to be popped.
   if (GetSize() == 0) return "";
-
+  
   const int start_pos = CountWhitespace();
   const int word_size = CountWordsize(start_pos);
-
+  
   // If the string is not all whitespace, save the word we cut off...
   cString rv("");
   if (word_size > 0) rv = Substring(start_pos, word_size);
-
+  
   // Trim off the front
   const int word_end = start_pos + word_size;
   const int new_start = word_end + CountWhitespace(word_end);
   InsertStr(0, NULL, 0, new_start);  // Insert null in place of old word.
-
+  
   return rv;
 }
 
@@ -431,7 +431,7 @@
     if( (*this)[pos] >= 'A' && (*this)[pos] <= 'Z' )
       (*this)[pos] += 'a' - 'A';
   }
-
+  
   return *this;
 }
 
@@ -442,7 +442,7 @@
     if( (*this)[pos] >= 'a' && (*this)[pos] <= 'z' )
       (*this)[pos] += 'A' - 'a';
   }
-
+  
   return *this;
 }
 
@@ -467,44 +467,44 @@
   int start_pos = CountWhitespace();
   int new_size = 0;
   bool ws = false;
-
+  
   // count the number of characters that we will need in the new string
   for( i=start_pos; i<GetSize(); ++i ){
     if( IsWhitespace(i) ){ // if it whitespace...
       if( ws == false ){     // if we arn't already in a whitespace block
-	ws = true;             // we are now in a whitespace block
+        ws = true;             // we are now in a whitespace block
       }
     }else{                 // it isn't whitespace, so count
       if( ws==true ){        // if there was a whitespace block
-	++new_size;            // inc once for the block
-	ws = false;
+        ++new_size;            // inc once for the block
+        ws = false;
       }
       ++new_size;
     }
   }
-
+  
   cString new_st(new_size);  // Allocate new string
-
+  
   // Copy over the characters
   // pos will be the location in new_st, while i is the index into this
   ws = false;
   for( i=start_pos; i<GetSize(); ++i ){
     if( IsWhitespace(i) ){ // if it whitespace...
       if( ws == false ){     // if we arn't already in a whitespace block
-	ws = true;             // we are now in a whitespace block
+        ws = true;             // we are now in a whitespace block
       }
     }else{                 // it isn't whitespace, so count
       if( ws==true ){        // if there was a whitespace block
-	new_st[pos] = ' ';     // put a space in for the whitespace block
-	++pos;                 // inc once for the block
-	ws = false;
+        new_st[pos] = ' ';     // put a space in for the whitespace block
+        ++pos;                 // inc once for the block
+        ws = false;
       }
       // new_st[pos] = (*this)[i]; // copy it & increment pos  @CAO prob in gcc
       new_st[pos] = value->GetData()[i]; // copy it & increment pos
       ++pos;
     }
   }
-
+  
   (*this) = new_st;  // assign the new_st to this
 }
 
@@ -561,12 +561,49 @@
 
 
 
+
+cString& cString::ParseEscapeSequences()
+{
+  int o_sz = GetSize();
+  char* newstr = new char[o_sz];
+  int sz = 0;
+  
+  for (int i = 0; i < o_sz; i++) {
+    if ((*value)[i] == '\\') { 
+      i++;
+      if (i == o_sz) break;
+      
+      switch ((*value)[i]) {
+        case 'b': newstr[sz++] = '\b'; break;
+        case 'f': newstr[sz++] = '\f'; break;
+        case 'n': newstr[sz++] = '\n'; break;
+        case 'r': newstr[sz++] = '\r'; break;
+        case 't': newstr[sz++] = '\t'; break;
+        case '\\':
+        case '\x22':
+        case '\'':
+        case '?':
+          newstr[sz++] = (*value)[i]; break;
+      }
+    } else {
+      newstr[sz++] = (*value)[i];
+    }
+  }
+  newstr[sz] = '\0';
+  
+  (*this) = newstr;
+  delete [] newstr;
+  
+  return *this;
+}
+
+
 // -- Internal Methods --
 
 cString & cString::AppendStr(const int in_size, const char * in)
 {
   assert (in_size == 0 || in != NULL); // NULL input string
-
+  
   // Allocate a new string
   cStringData * new_value = new cStringData(GetSize()+in_size);
   assert (new_value != NULL);       // Memory Allocation Error: Out of Memory
@@ -583,12 +620,12 @@
 
 
 cString & cString::InsertStr(const int in_size, const char * in,
-			     int pos, int excise )
+                             int pos, int excise )
 {
   // Inserts 'in' (of length 'in_size') at postition 'pos'
   // Also excises 'excise' characters from 'pos'
   // If 'in_size'==0 then 'in' can == NULL and only excise happens
-
+  
   // Validate inputs:
   assert (in_size >= 0);               // Negative input size
   assert (pos >= 0);                   // Negative position
@@ -597,12 +634,12 @@
   assert (excise <= GetSize()-pos);    // Excise number too large
   assert (excise > 0 || in_size > 0);  // Make sure a change is made!
   assert (in_size == 0 || in != NULL); // NULL input string
-
+  
   // Allocate a new string
   const int new_size = GetSize() + in_size - excise;
   cStringData * new_value = new cStringData(new_size);
   assert (new_value != NULL);  // Memory Allocation Error: Out of Memory
-
+  
   for(int i = 0; i < pos; ++i ){             // Copy self up to pos
     (*new_value)[i] = this->operator[](i);
   }
@@ -613,7 +650,7 @@
   for(int i=pos+excise; i<GetSize(); ++i ){  // Copy rest of self
     (*new_value)[i+in_size-excise] = this->operator[](i);
   }
-
+  
   TakeValue(new_value);                      // Reassing data to new data
   return(*this);
 }
@@ -622,28 +659,28 @@
 cString cString::EjectStr(int pos, int excise )
 {
   // Delete excise characters at pos and return the substring.
-
+  
   // Validate inputs:
   assert (pos >= 0);                   // Negative position
   assert (pos <= GetSize());           // Position past end of string
   assert (excise > 0);                 // Must excise something...
   assert (excise <= GetSize()-pos);    // Excise number too large
-
+  
   // Collect substring to output.
   cString out_string(Substring(pos, excise));
-
+  
   // Allocate a new string
   const int new_size = GetSize() - excise;
   cStringData * new_value = new cStringData(new_size);
   assert (new_value != NULL);  // Memory Allocation Error: Out of Memory
-
+  
   for(int i = 0; i < pos; i++){             // Copy self up to pos
     (*new_value)[i] = this->operator[](i);
   }
   for(int i=pos+excise; i<GetSize(); ++i ){  // Copy post-excise self
     (*new_value)[i-excise] = this->operator[](i);
   }
-
+  
   TakeValue(new_value);                      // Reassing data to new data
   return out_string;
 }
@@ -653,16 +690,16 @@
 {
   assert (pos>=0);         // Negative position
   assert (pos<=GetSize()); // Position past end of string
-
+  
   while (pos < GetSize()) {
     if( GetSize()-pos < in_size ) return -1; // Too near this string's end.
     if( (*this)[pos] == in[0] ){
       // see if we have found the string...
       int i;
       for( i = 1; i < in_size; i++ ){
-	assert (pos+i < GetSize()); // Reached end of (*this) in Find
-	assert (in[i] != '\0');     // Reached end of 'in' in Find
-	if( (*this)[pos + i] != in[i] ) break; // Match failure!
+        assert (pos+i < GetSize()); // Reached end of (*this) in Find
+        assert (in[i] != '\0');     // Reached end of 'in' in Find
+        if( (*this)[pos + i] != in[i] ) break; // Match failure!
       }
       // If we have made it fully through the loop, we have found a match!
       if( i == in_size ) return pos;

Modified: development/source/tools/cString.h
===================================================================
--- development/source/tools/cString.h	2008-03-17 19:57:20 UTC (rev 2459)
+++ development/source/tools/cString.h	2008-03-17 21:20:07 UTC (rev 2460)
@@ -214,24 +214,18 @@
 
 
   // Additional modifiers
-  cString & Set(const char * fmt, ...);
+  cString& Set(const char * fmt, ...);
 
-  cString & Insert(const char in, int pos=0, int excise=0){
-    return InsertStr(1, &in, pos, excise); }
-  cString & Insert(const char * in, int pos=0, int excise=0){
-    return InsertStr(strlen(in), in, pos, excise); }
-  cString & Insert(const cString & in, int pos=0, int excise=0){
-    return InsertStr(in.GetSize(), in, pos, excise); }
+  cString& Insert(const char in, int pos = 0, int excise = 0) { return InsertStr(1, &in, pos, excise); }
+  cString& Insert(const char* in, int pos = 0, int excise = 0) { return InsertStr(strlen(in), in, pos, excise); }
+  cString& Insert(const cString& in, int pos = 0, int excise = 0) { return InsertStr(in.GetSize(), in, pos, excise); }
 
 
   // Removes 'size' characters from 'pos' (default size = to end of string)
-  cString & Clip(int pos, int size = -1 /*end of string*/ ){
-    if( size < 0 ) size = GetSize()-pos;
-    return InsertStr(0, NULL, pos, size); }
-  cString & ClipFront(int size){  // Clip off first 'clip_size' chars
-    return InsertStr(0, NULL, 0, size); }
-  cString & ClipEnd(int size){    // Clip off last 'clip_size' chars
-    return InsertStr(0, NULL, GetSize()-size, size); }
+  cString& Clip(int pos, int size = -1 /*end of string*/ )
+    { if( size < 0 ) size = GetSize() - pos; return InsertStr(0, NULL, pos, size); }
+  cString& ClipFront(int size) { /* Clip off first 'clip_size' chars */ return InsertStr(0, NULL, 0, size); }
+  cString& ClipEnd(int size) { /* Clip off last 'clip_size' chars */ return InsertStr(0, NULL, GetSize() - size, size); }
   
   /**
    * Find and replace a substring in the string with a different substring.
@@ -242,7 +236,7 @@
    * @param new_string The replacement.
    * @param pos The position at which the search should start.
    **/
-  int Replace(const cString & old_st, const cString & new_st, int pos=0);
+  int Replace(const cString& old_st, const cString& new_st, int pos = 0);
 
   cString Pop(const char delim);  // Remove and return up to delim char
   
@@ -258,7 +252,7 @@
    *
    * @return The removed line.
    **/
-  cString PopLine(){ return Pop('\n'); } 
+  cString PopLine() { return Pop('\n'); } 
   
   /**
    * Remove begining whitespace.
@@ -319,11 +313,14 @@
    * Get rid of one character at a specific location
    **/
   void RemovePos(int pos);
+  
+  
+  // Parse for and replace escape sequences within the string
+  cString& ParseEscapeSequences();
+  
 
   // Individal Char Access
-  inline char operator[] (int index) const {
-    return static_cast<char>((*value)[index]);
-  }
+  inline char operator[] (int index) const { return static_cast<char>((*value)[index]); }
   cCharProxy operator[] (int index) { return cCharProxy(*this,index); }
 
 




More information about the Avida-cvs mailing list