Skip to content

Commit 923eb91

Browse files
committed
mainly JSON tidy-up
1 parent 94c54f9 commit 923eb91

4 files changed

Lines changed: 81 additions & 56 deletions

File tree

Foundation/include/Poco/Buffer.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ class Buffer
166166
/// Resizes this buffer and appends the argument buffer.
167167
{
168168
if (0 == sz) return;
169-
std::size_t oldSize = _used;
170169
resize(_used + sz, true);
171-
std::memcpy(_ptr + oldSize, buf, sz);
170+
std::memcpy(_ptr + _used - sz, buf, sz);
171+
}
172+
173+
void append(T val)
174+
/// Resizes this buffer by one element and appends the argument value.
175+
{
176+
resize(_used + 1, true);
177+
_ptr[_used - 1] = val;
172178
}
173179

174180
void append(const Buffer& buf)

Foundation/testsuite/src/CoreTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,21 @@ void CoreTest::testBuffer()
313313
k.append("hello", 5);
314314
assert ( !std::memcmp(&j[0], "hello", 5) );
315315
assert ( !std::memcmp(k.begin(), "hellohello", 10) );
316+
k.append('w');
317+
assert (k.size() == 11);
318+
assert ( !std::memcmp(k.begin(), "hellohellow", k.size()) );
319+
k.append('o');
320+
assert (k.size() == 12);
321+
assert ( !std::memcmp(k.begin(), "hellohellowo", k.size()) );
322+
k.append('r');
323+
assert (k.size() == 13);
324+
assert ( !std::memcmp(k.begin(), "hellohellowor", k.size()) );
325+
k.append('l');
326+
assert (k.size() == 14);
327+
assert ( !std::memcmp(k.begin(), "hellohelloworl", k.size()) );
328+
k.append('d');
329+
assert (k.size() == 15);
330+
assert ( !std::memcmp(k.begin(), "hellohelloworld", k.size()) );
316331
}
317332

318333

JSON/include/Poco/JSON/Parser.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class JSON_API Parser
453453
// closing comment
454454
case CE:
455455
_comment = 0;
456-
poco_assert(_parseBufferCount == 0);
456+
poco_assert(_parseBuffer.size() == 0);
457457
poco_assert(_type == JSON_T_NONE);
458458
_state = _beforeCommentState;
459459
break;
@@ -463,7 +463,7 @@ class JSON_API Parser
463463
if (!_allowComments) return false;
464464
parseBufferPopBackChar();
465465
parseBuffer();
466-
poco_assert(_parseBufferCount == 0);
466+
poco_assert(_parseBuffer.size() == 0);
467467
poco_assert(_type != JSON_T_STRING);
468468
switch (_stack[_top])
469469
{
@@ -553,8 +553,7 @@ class JSON_API Parser
553553

554554
if (_pHandler)
555555
{
556-
std::string value(_parseBuffer.begin(), _parseBufferCount);
557-
_pHandler->key(value);
556+
_pHandler->key(std::string(_parseBuffer.begin(), _parseBuffer.size()));
558557
}
559558
clearBuffer();
560559
break;
@@ -634,7 +633,6 @@ class JSON_API Parser
634633
int _top;
635634
BufType _stack;
636635
BufType _parseBuffer;
637-
std::size_t _parseBufferCount;
638636
char _decimalPoint;
639637
bool _allowNullByte;
640638
bool _allowComments;

JSON/src/Parser.cpp

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ Parser::Parser(const Handler::Ptr& pHandler, std::size_t bufSize) :
141141
_top(-1),
142142
_stack(JSON_PARSER_STACK_SIZE),
143143
_parseBuffer(bufSize),
144-
_parseBufferCount(0),
145144
_decimalPoint('.'),
146145
_allowNullByte(true),
147146
_allowComments(false)
148147
{
148+
_parseBuffer.resize(0);
149149
push(MODE_DONE);
150150
}
151151

@@ -163,12 +163,10 @@ void Parser::reset()
163163
_escaped = 0;
164164
_utf16HighSurrogate = 0;
165165
_top = -1;
166-
_parseBufferCount = 0;
167166

168167
_stack.clear();
169-
_parseBuffer.clear();
168+
_parseBuffer.resize(0);
170169
push(MODE_DONE);
171-
clearBuffer();
172170
if (_pHandler) _pHandler->reset();
173171
}
174172

@@ -241,25 +239,23 @@ bool Parser::pop(int mode)
241239

242240
void Parser::clearBuffer()
243241
{
244-
_parseBufferCount = 0;
245-
_parseBuffer[0] = 0;
242+
_parseBuffer.resize(0);
246243
}
247244

248245

249246
void Parser::parseBufferPopBackChar()
250247
{
251-
poco_assert(_parseBufferCount >= 1);
252-
--_parseBufferCount;
253-
_parseBuffer[_parseBufferCount] = 0;
248+
poco_assert(_parseBuffer.size() >= 1);
249+
_parseBuffer.resize(_parseBuffer.size() - 1);
254250
}
255251

256252

257253
void Parser::parseBufferPushBackChar(char c)
258254
{
259-
if (_parseBufferCount + 1 >= _parseBuffer.size())
260-
growBuffer();
261-
_parseBuffer[_parseBufferCount++] = c;
262-
_parseBuffer[_parseBufferCount] = 0;
255+
if (_parseBuffer.size() + 1 >= _parseBuffer.capacity())
256+
_parseBuffer.resize(_parseBuffer.capacity() * 2);
257+
258+
_parseBuffer.append(c);
263259
}
264260

265261

@@ -329,9 +325,8 @@ Parser::CharIntType Parser::decodeUnicodeChar()
329325
char* p;
330326
int trail_bytes;
331327

332-
poco_assert(_parseBufferCount >= 6);
333-
334-
p = &_parseBuffer[_parseBufferCount - 4];
328+
poco_assert(_parseBuffer.size() >= 6);
329+
p = &_parseBuffer[_parseBuffer.size() - 4];
335330

336331
for (i = 12; i >= 0; i -= 4, ++p) {
337332
unsigned x = *p;
@@ -352,44 +347,57 @@ Parser::CharIntType Parser::decodeUnicodeChar()
352347
if ( !_allowNullByte && uc == 0 ) return 0; // Null byte not allowed
353348

354349
// clear UTF-16 char from buffer
355-
_parseBufferCount -= 6;
356-
_parseBuffer[_parseBufferCount] = 0;
350+
_parseBuffer.resize(_parseBuffer.size() - 6);
357351

358352
// attempt decoding
359-
if (_utf16HighSurrogate) {
360-
if (IS_LOW_SURROGATE(uc)) {
353+
if (_utf16HighSurrogate)
354+
{
355+
if (IS_LOW_SURROGATE(uc))
356+
{
361357
uc = DECODE_SURROGATE_PAIR(_utf16HighSurrogate, uc);
362358
trail_bytes = 3;
363359
_utf16HighSurrogate = 0;
364-
} else {
360+
}
361+
else
362+
{
365363
// high surrogate without a following low surrogate
366364
return 0;
367365
}
368-
} else {
369-
if (uc < 0x80) {
366+
}
367+
else
368+
{
369+
if (uc < 0x80)
370+
{
370371
trail_bytes = 0;
371-
} else if (uc < 0x800) {
372+
}
373+
else if (uc < 0x800)
374+
{
372375
trail_bytes = 1;
373-
} else if (IS_HIGH_SURROGATE(uc)) {
376+
}
377+
else if (IS_HIGH_SURROGATE(uc))
378+
{
374379
// save the high surrogate and wait for the low surrogate
375380
_utf16HighSurrogate = uc;
376381
return 1;
377-
} else if (IS_LOW_SURROGATE(uc)) {
382+
}
383+
else if (IS_LOW_SURROGATE(uc))
384+
{
378385
// low surrogate without a preceding high surrogate
379386
return 0;
380-
} else {
387+
}
388+
else
389+
{
381390
trail_bytes = 2;
382391
}
383392
}
384393

385-
_parseBuffer[_parseBufferCount++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
394+
_parseBuffer.append((char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]));
386395

387-
for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
388-
_parseBuffer[_parseBufferCount++] = (char) (((uc >> i) & 0x3F) | 0x80);
396+
for (i = trail_bytes * 6 - 6; i >= 0; i -= 6)
397+
{
398+
_parseBuffer.append((char) (((uc >> i) & 0x3F) | 0x80));
389399
}
390400

391-
_parseBuffer[_parseBufferCount] = 0;
392-
393401
return 1;
394402
}
395403

@@ -424,9 +432,9 @@ void Parser::parseBuffer()
424432
case JSON_T_FLOAT:
425433
{
426434
// Float can't end with a dot
427-
if ( _parseBuffer[_parseBufferCount-1] == '.' ) throw SyntaxException("JSON syntax error");
435+
if (_parseBuffer[_parseBuffer.size() - 1] == '.' ) throw SyntaxException("JSON syntax error");
428436

429-
double float_value = NumberParser::parseFloat(_parseBuffer.begin());
437+
double float_value = NumberParser::parseFloat(std::string(_parseBuffer.begin(), _parseBuffer.size()));
430438
_pHandler->value(float_value);
431439
break;
432440
}
@@ -435,7 +443,7 @@ void Parser::parseBuffer()
435443
#if defined(POCO_HAVE_INT64)
436444
try
437445
{
438-
Int64 value = NumberParser::parse64(_parseBuffer.begin());
446+
Int64 value = NumberParser::parse64(std::string(_parseBuffer.begin(), _parseBuffer.size()));
439447
// if number is 32-bit, then handle as such
440448
if (value > std::numeric_limits<int>::max()
441449
|| value < std::numeric_limits<int>::min() )
@@ -450,7 +458,7 @@ void Parser::parseBuffer()
450458
// try to handle error as unsigned in case of overflow
451459
catch ( const SyntaxException& )
452460
{
453-
UInt64 value = NumberParser::parseUnsigned64(_parseBuffer.begin());
461+
UInt64 value = NumberParser::parseUnsigned64(std::string(_parseBuffer.begin(), _parseBuffer.size()));
454462
// if number is 32-bit, then handle as such
455463
if ( value > std::numeric_limits<unsigned>::max() )
456464
{
@@ -464,22 +472,21 @@ void Parser::parseBuffer()
464472
#else
465473
try
466474
{
467-
int value = NumberParser::parse(_parseBuffer.begin());
475+
int value = NumberParser::parse(std::string(_parseBuffer.begin(), _parseBuffer.size()));
468476
_pHandler->value(value);
469477
}
470478
// try to handle error as unsigned in case of overflow
471479
catch ( const SyntaxException& )
472480
{
473-
unsigned value = NumberParser::parseUnsigned(_parseBuffer.begin());
481+
unsigned value = NumberParser::parseUnsigned(std::string(_parseBuffer.begin(), _parseBuffer.size()));
474482
_pHandler->value(value);
475483
}
476484
#endif
477485
}
478486
break;
479487
case JSON_T_STRING:
480488
{
481-
std::string str(_parseBuffer.begin(), _parseBufferCount);
482-
_pHandler->value(str);
489+
_pHandler->value(std::string(_parseBuffer.begin(), _parseBuffer.size()));
483490
break;
484491
}
485492
}
@@ -498,35 +505,34 @@ int Parser::utf8CheckFirst(char byte)
498505

499506
if (0x80 <= u && u <= 0xBF)
500507
{
501-
/* second, third or fourth byte of a multi-byte
502-
sequence, i.e. a "continuation byte" */
508+
// second, third or fourth byte of a multi-byte
509+
// sequence, i.e. a "continuation byte"
503510
return 0;
504511
}
505512
else if(u == 0xC0 || u == 0xC1)
506513
{
507-
/* overlong encoding of an ASCII byte */
514+
// overlong encoding of an ASCII byte
508515
return 0;
509516
}
510517
else if(0xC2 <= u && u <= 0xDF)
511518
{
512-
/* 2-byte sequence */
519+
// 2-byte sequence
513520
return 2;
514521
}
515522
else if(0xE0 <= u && u <= 0xEF)
516523
{
517-
/* 3-byte sequence */
524+
// 3-byte sequence
518525
return 3;
519526
}
520527
else if(0xF0 <= u && u <= 0xF4)
521528
{
522-
/* 4-byte sequence */
529+
// 4-byte sequence
523530
return 4;
524531
}
525532
else
526533
{
527-
/* u >= 0xF5 */
528-
/* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
529-
UTF-8 */
534+
// u >= 0xF5
535+
// Restricted (start of 4-, 5- or 6-byte sequence) or invalid UTF-8
530536
return 0;
531537
}
532538
}

0 commit comments

Comments
 (0)