bool
Reader::decodeUnicodeCodePoint( Token &token, 
                                     Location &current, 
                                     Location end, 
                                     unsigned int &unicode )
{

   if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) )
      return false;
   if (unicode >= 0xD800 && unicode <= 0xDBFF)
   {
      // surrogate pairs
      if (end - current < 6)
         return addError( "additional six characters expected to parse unicode surrogate pair.", token, current );
      unsigned int surrogatePair;
      if (*(current++) == '\\' && *(current++)== 'u')
      {
         if (decodeUnicodeEscapeSequence( token, current, end, surrogatePair ))
         {
            unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
         } 
         else
            return false;
      } 
      else
         return addError( "expecting another \\u token to begin the second half of a unicode surrogate pair", token, current );
   }
   return true;
}
Example #2
0
  bool 
  Reader::decodeString (Token &token, std::string &decoded)
  {
    Location current = token.start_ + 1; // skip '"'
    Location end = token.end_ - 1;       // do not include '"'
    decoded.reserve (long (end - current));

    while (current != end)
      {
	char c = *current++;
	if (expect_false (c == '"'))
	  break;
	else if (expect_false (c == '\\'))
	  {
	    if (expect_false (current == end))
	      return addError ("Empty escape sequence in string", token, current);
	    char escape = *current++;
	    switch (escape)
	      {
	      case '"':
	      case '/':
	      case '\\': decoded += escape; break;

	      case 'b': decoded += '\010'; break;
	      case 't': decoded += '\011'; break;
	      case 'n': decoded += '\012'; break;
	      case 'f': decoded += '\014'; break;
	      case 'r': decoded += '\015'; break;
	      case 'u':
		{
		  unsigned unicode;
		  if (!decodeUnicodeEscapeSequence (token, current, end, unicode))
		    return false;
		  // @todo encode unicode as utf8.
		  // @todo remember to alter the writer too.
		}
		break;
	      default:
		return addError ("Bad escape sequence in string", token, current);
	      }
	  }
	else
	  {
	    decoded += c;
	  }
      }

    return true;
  }
bool 
Reader::decodeString( Token &token, std::string &decoded )
{
   decoded.reserve( token.end_ - token.start_ - 2 );
   Location current = token.start_ + 1; // skip '"'
   Location end = token.end_ - 1;      // do not include '"'
   while ( current != end )
   {
      Char c = *current++;
      if ( c == '"' )
         break;
      else if ( c == '\\' )
      {
         if ( current == end )
            return addError( "Empty escape sequence in string", token, current );
         Char escape = *current++;
         switch ( escape )
         {
         case '"': decoded += '"'; break;
         case '/': decoded += '/'; break;
         case '\\': decoded += '\\'; break;
         case 'b': decoded += '\b'; break;
         case 'f': decoded += '\f'; break;
         case 'n': decoded += '\n'; break;
         case 'r': decoded += '\r'; break;
         case 't': decoded += '\t'; break;
         case 'u':
            {
               unsigned int unicode;
               if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) )
                  return false;
               // @todo encode unicode as utf8.
	       // @todo remember to alter the writer too.
            }
            break;
         default:
            return addError( "Bad escape sequence in string", token, current );
         }
      }
      else
      {
         decoded += c;
      }
   }
   return true;
}