inline void flush( Polygon &polyline ) { appendTo( y1, polyline ); if ( y2 > y1 ) qSwap( yMin, yMax ); if ( yMax != y1 ) appendTo( yMax, polyline ); if ( yMin != yMax ) appendTo( yMin, polyline ); if ( y2 != yMin ) appendTo( y2, polyline ); }
inline void flush( Polygon &polyline ) { appendTo( x1, polyline ); if ( x2 > x1 ) qSwap( xMin, xMax ); if ( xMax != x1 ) appendTo( xMax, polyline ); if ( xMin != xMax ) appendTo( xMin, polyline ); if ( x2 != xMin ) appendTo( x2, polyline ); }
Vector<UChar> String::charactersWithNullTermination() const { if (!m_impl) return Vector<UChar>(); Vector<UChar> result; result.reserveInitialCapacity(length() + 1); appendTo(result); result.append('\0'); return result; }
int main(int argc, char const *argv[]) { list* linked = init_list(); int i; for (i = 1; i < 6; i++) { appendTo(linked, newnode(i)); } print_list(linked); delAt(linked, -1); delAt(linked, 5); print_list(linked); delAt(linked, 3); print_list(linked); return 0; }
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode* ans = NULL; ListNode* ret = NULL; int increment = 0; while(l1 || l2 || increment) { int value = increment; if (l1) { value += l1->val; l1 = l1->next; } if (l2) { value += l2->val; l2 = l2->next; } increment = value / 10; value %= 10; bool isHead; ans = appendTo(ans, value, isHead); if (isHead) { ret = ans; } } return ret; }
LBasicBlock Output::appendTo(LBasicBlock block, LBasicBlock nextBlock) { appendTo(block); return insertNewBlocksBefore(nextBlock); }
void SimplePatternFormatterTest::TestManyPlaceholders() { UErrorCode status = U_ZERO_ERROR; SimplePatternFormatter fmt; fmt.compile( "Templates {2}{1}{5} and {4} are out of order.", status); assertSuccess("Status", status); assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(2)); assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount()); UnicodeString values[] = { "freddy", "tommy", "frog", "billy", "leg", "{0}"}; UnicodeString *params[] = { &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]}; int32_t offsets[6]; int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27}; UnicodeString appendTo("Prefix: "); assertEquals( "format", "Prefix: Templates frogtommy{0} and leg are out of order.", fmt.format( params, UPRV_LENGTHOF(params), appendTo, offsets, UPRV_LENGTHOF(offsets), status)); assertSuccess("Status", status); for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) { if (expectedOffsets[i] != offsets[i]) { errln("Expected %d, got %d", expectedOffsets[i], offsets[i]); } } appendTo.remove(); // Not having enough placeholder params results in error. fmt.format( params, UPRV_LENGTHOF(params) - 1, appendTo, offsets, UPRV_LENGTHOF(offsets), status); if (status != U_ILLEGAL_ARGUMENT_ERROR) { errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); } // Ensure we don't write to offsets array beyond its length. status = U_ZERO_ERROR; offsets[UPRV_LENGTHOF(offsets) - 1] = 289; appendTo.remove(); fmt.format( params, UPRV_LENGTHOF(params), appendTo, offsets, UPRV_LENGTHOF(offsets) - 1, status); assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) - 1]); // Test assignment SimplePatternFormatter s; s = fmt; appendTo.remove(); assertEquals( "Assignment", "Templates frogtommy{0} and leg are out of order.", s.format( params, UPRV_LENGTHOF(params), appendTo, NULL, 0, status)); // Copy constructor SimplePatternFormatter r(fmt); appendTo.remove(); assertEquals( "Copy constructor", "Templates frogtommy{0} and leg are out of order.", r.format( params, UPRV_LENGTHOF(params), appendTo, NULL, 0, status)); r.compile("{0} meter", status); assertEquals("PlaceholderCount", 1, r.getPlaceholderCount()); appendTo.remove(); assertEquals( "Replace with new compile", "freddy meter", r.format("freddy", appendTo, status)); r.compile("{0}, {1}", status); assertEquals("PlaceholderCount", 2, r.getPlaceholderCount()); appendTo.remove(); assertEquals( "2 arg", "foo, bar", r.format("foo", "bar", appendTo, status)); r.compile("{0}, {1} and {2}", status); assertEquals("PlaceholderCount", 3, r.getPlaceholderCount()); appendTo.remove(); assertEquals( "3 arg", "foo, bar and baz", r.format("foo", "bar", "baz", appendTo, status)); assertSuccess("Status", status); }
char *formatLine( const date_t gregDate, const date_t hebDate, const char *text, char *output, size_t maxOutputLen ) { size_t formatLen = strlen(formatString); int allocLength = 200 + formatLen * 2; char autoBuf[allocLength]; char *buf = autoBuf; char * bufEnd = buf + allocLength; char * bufIter = buf; char * formatEnd = formatString+ formatLen; const char *formatIter; if( ! buf ) die( "unable to allocate %s bytes to format line", hc_itoa(strlen(formatString)*2)); *buf = '\0'; if( bufEnd > buf ) *(bufEnd - 1) = '\0'; for( formatIter = formatString; formatIter < formatEnd && *formatIter; formatIter++) { switch( *formatIter ) { case '\\': formatIter++; if( formatIter >=formatEnd || ! *formatIter) break; switch( *formatIter ) { case 'n': appendTo(&buf, &bufEnd, &bufIter, "\n"); break; case 'b': appendTo(&buf, &bufEnd, &bufIter, "\b"); break; case 'e': appendTo(&buf, &bufEnd, &bufIter, "\033"); break; case 'f': appendTo(&buf, &bufEnd, &bufIter, "\f"); break; case 'r': appendTo(&buf, &bufEnd, &bufIter, "\r"); break; case 't': appendTo(&buf, &bufEnd, &bufIter, "\t"); break; case '\\': appendTo(&buf, &bufEnd, &bufIter, "\\"); break; } break; case '%': formatIter++; if( formatIter >=formatEnd || ! *formatIter) break; switch( *formatIter ) { case '%': appendTo(&buf, &bufEnd, &bufIter, "%"); break; case 'a': /* abbreviated weekday name */ appendTo(&buf, &bufEnd, &bufIter, ShortDayNames[dayOfWeek(gregDate)]); break; case 'Y': /* year */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(gregDate.yy)); break; case 'm': /* month */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(gregDate.mm)); break; case 'd': /* day of month */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(gregDate.dd)); break; case 'Q': /* hebrew year */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(hebDate.yy)); break; case 'q': /* hebrew month */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(hebDate.mm)); break; case 'k': /* hebrew day */ appendTo(&buf, &bufEnd, &bufIter, hc_itoa(hebDate.dd)); break; case 'J': /* julian day number */ { char numBuf[20]; snprintf(numBuf, 20, "%ld",greg2abs(gregDate)); numBuf[19] = '\0'; appendTo(&buf, &bufEnd, &bufIter, numBuf); break; } case 't': /* holiday text */ appendTo(&buf, &bufEnd, &bufIter, text); break; /* default: */ /* die ("unknown %% escape: %s",formatIter); */ } break; default: *bufIter++ = *formatIter; break; } } *bufIter++ = '\0'; strncpy( output, buf, maxOutputLen); output[maxOutputLen-1] ='\0'; return output; }
std::string GlogStyleFormatter::formatMessage( const LogMessage& message, const LogCategory* /* handlerCategory */) { // Get the local time info struct tm ltime; auto timeSinceEpoch = message.getTimestamp().time_since_epoch(); auto epochSeconds = std::chrono::duration_cast<std::chrono::seconds>(timeSinceEpoch); std::chrono::microseconds usecs = std::chrono::duration_cast<std::chrono::microseconds>(timeSinceEpoch) - epochSeconds; time_t unixTimestamp = epochSeconds.count(); if (!localtime_r(&unixTimestamp, <ime)) { memset(<ime, 0, sizeof(ltime)); } auto basename = message.getFileBaseName(); auto headerFormatter = folly::format( "{}{:02d}{:02d} {:02d}:{:02d}:{:02d}.{:06d} {:5d} {}:{}] ", getGlogLevelName(message.getLevel())[0], ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, usecs.count(), message.getThreadID(), basename, message.getLineNumber()); // TODO: Support including thread names and thread context info. // The fixed portion of the header takes up 31 bytes. // // The variable portions that we can't account for here include the line // number and the thread ID (just in case it is larger than 6 digits long). // Here we guess that 40 bytes will be long enough to include room for this. // // If this still isn't long enough the string will grow as necessary, so the // code will still be correct, but just slightly less efficient than if we // had allocated a large enough buffer the first time around. size_t headerLengthGuess = 40 + basename.size(); // Format the data into a buffer. std::string buffer; StringPiece msgData{message.getMessage()}; if (message.containsNewlines()) { // If there are multiple lines in the log message, add a header // before each one. std::string header; header.reserve(headerLengthGuess); headerFormatter.appendTo(header); // Make a guess at how many lines will be in the message, just to make an // initial buffer allocation. If the guess is too small then the string // will reallocate and grow as necessary, it will just be slightly less // efficient than if we had guessed enough space. size_t numLinesGuess = 4; buffer.reserve(((header.size() + 1) * numLinesGuess) + msgData.size()); size_t idx = 0; while (true) { auto end = msgData.find('\n', idx); if (end == StringPiece::npos) { end = msgData.size(); } buffer.append(header); auto line = msgData.subpiece(idx, end - idx); buffer.append(line.data(), line.size()); buffer.push_back('\n'); if (end == msgData.size()) { break; } idx = end + 1; } } else { buffer.reserve(headerLengthGuess + msgData.size()); headerFormatter.appendTo(buffer); buffer.append(msgData.data(), msgData.size()); buffer.push_back('\n'); } return buffer; }