static PARCBufferComposer * _buildStringString(const PARCJSONValue *value, PARCBufferComposer *composer, bool compact) { parcBufferComposer_PutChar(composer, '"'); while (parcBuffer_Remaining(value->value.string)) { uint8_t c = parcBuffer_GetUint8(value->value.string); if (c == '"') { parcBufferComposer_PutString(composer, "\\\""); } else if (c == '\b') { parcBufferComposer_PutString(composer, "\\b"); } else if (c == '\f') { parcBufferComposer_PutString(composer, "\\f"); } else if (c == '\n') { parcBufferComposer_PutString(composer, "\\n"); } else if (c == '\r') { parcBufferComposer_PutString(composer, "\\r"); } else if (c == '\t') { parcBufferComposer_PutString(composer, "\\t"); } else if ((c == '/') && !compact) { parcBufferComposer_PutString(composer, "\\/"); } else if (c == '\\') { parcBufferComposer_PutString(composer, "\\\\"); } else { parcBufferComposer_PutChar(composer, c); } } parcBuffer_Rewind(value->value.string); parcBufferComposer_PutChar(composer, '"'); return composer; }
static PARCBufferComposer * _uriSegment_BuildString(const PARCURISegment *segment, PARCBufferComposer *composer) { assertNotNull(composer, "Parameter must be a non-null pointer to a PARCBufferComposer."); for (size_t i = 0; i < parcBuffer_Limit(segment->buffer) && composer != NULL; i++) { unsigned char c = parcBuffer_GetAtIndex(segment->buffer, i); if (uriPlainSegmentChar(c)) { parcBufferComposer_PutChar(composer, c); } else { parcBufferComposer_PutChar(composer, '%'); parcBufferComposer_PutChar(composer, _toHexDigit(c >> 4)); parcBufferComposer_PutChar(composer, _toHexDigit(c)); } } return composer; }
PARCBuffer * parcJSONParser_ParseString(PARCJSONParser *parser) { PARCBuffer *result = NULL; PARCBuffer *buffer = _getBuffer(parser); if (parcBuffer_GetUint8(buffer) == '"') { // skip the initial '"' character starting the string. PARCBufferComposer *composer = parcBufferComposer_Create(); while (parcBuffer_Remaining(buffer)) { uint8_t c = parcBuffer_GetUint8(buffer); if (c == '"') { // This is the only successful way to exit this while loop. result = parcBufferComposer_ProduceBuffer(composer); break; } else if (c == '\\') { c = parcBuffer_GetUint8(buffer); if (c == '"') { // this special character passes directly into the composed string. } else if (c == '\\') { // this special character passes directly into the composed string. } else if (c == '/') { // this special character passes directly into the composed string. } else if (c == 'b') { c = '\b'; } else if (c == 'f') { c = '\f'; } else if (c == 'n') { c = '\n'; } else if (c == 'r') { c = '\r'; } else if (c == 't') { c = '\t'; } else if (c == 'u') { // Not supporting unicode at this point. trapNotImplemented("Unicode is not supported."); } } else if (iscntrl(c)) { // !! Syntax Error. break; } parcBufferComposer_PutChar(composer, c); } parcBufferComposer_Release(&composer); } return result; }
PARCBufferComposer * parcURIPath_BuildString(const PARCURIPath *path, PARCBufferComposer *composer) { size_t nSegments = parcArrayList_Size(path->segments); for (size_t i = 0; i < nSegments && composer != NULL; i++) { if (parcURISegment_BuildString(parcURIPath_Get(path, i), composer) == NULL) { composer = NULL; } if (i < (nSegments - 1)) { composer = parcBufferComposer_PutChar(composer, '/'); } } return composer; }