void ErrMsg_set_with_errno(const char *fmt, ...) { int cur_errno = errno; CharBuf *buf = CB_new(0); va_list args; va_start(args, fmt); CB_VCatF(buf, fmt, args); va_end(args); CB_Cat_Trusted_Utf8(buf, ": ", 2); const char *msg = ErrMsg_strerror(cur_errno); if (msg != NULL) { CB_Cat_Trusted_Utf8(buf, msg, strlen(msg)); } else { CB_catf(buf, "Unknown error: %i32", (int32_t)cur_errno); } Err_set_error(Err_new(CB_Yield_String(buf))); DECREF(buf); }
static String* S_encode_entities(String *text, CharBuf *buf) { StringIterator *iter = Str_Top(text); size_t space = 0; const int MAX_ENTITY_BYTES = 9; // &#dddddd; // Scan first so that we only allocate once. int32_t code_point; while (STRITER_DONE != (code_point = StrIter_Next(iter))) { if (code_point > 127 || (!isgraph(code_point) && !isspace(code_point)) || code_point == '<' || code_point == '>' || code_point == '&' || code_point == '"' ) { space += MAX_ENTITY_BYTES; } else { space += 1; } } CB_Grow(buf, space); CB_Set_Size(buf, 0); DECREF(iter); iter = Str_Top(text); while (STRITER_DONE != (code_point = StrIter_Next(iter))) { if (code_point > 127 || (!isgraph(code_point) && !isspace(code_point)) ) { CB_catf(buf, "&#%u32;", code_point); } else if (code_point == '<') { CB_Cat_Trusted_Utf8(buf, "<", 4); } else if (code_point == '>') { CB_Cat_Trusted_Utf8(buf, ">", 4); } else if (code_point == '&') { CB_Cat_Trusted_Utf8(buf, "&", 5); } else if (code_point == '"') { CB_Cat_Trusted_Utf8(buf, """, 6); } else { CB_Cat_Char(buf, code_point); } } DECREF(iter); return CB_To_String(buf); }
static void test_Cat(TestBatchRunner *runner) { String *wanted = Str_newf("a%s", smiley); CharBuf *got = S_get_cb(""); CB_Cat(got, wanted); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat"); DECREF(got); got = S_get_cb("a"); CB_Cat_Char(got, 0x263A); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Char"); DECREF(got); got = S_get_cb("a"); CB_Cat_Utf8(got, smiley, smiley_len); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Utf8"); DECREF(got); got = S_get_cb("a"); CB_Cat_Trusted_Utf8(got, smiley, smiley_len); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Trusted_Utf8"); DECREF(got); DECREF(wanted); }
String* PhraseQuery_To_String_IMP(PhraseQuery *self) { PhraseQueryIVARS *const ivars = PhraseQuery_IVARS(self); uint32_t num_terms = Vec_Get_Size(ivars->terms); CharBuf *buf = CB_new_from_str(ivars->field); CB_Cat_Trusted_Utf8(buf, ":\"", 2); for (uint32_t i = 0; i < num_terms; i++) { Obj *term = Vec_Fetch(ivars->terms, i); String *term_string = Obj_To_String(term); CB_Cat(buf, term_string); DECREF(term_string); if (i < num_terms - 1) { CB_Cat_Trusted_Utf8(buf, " ", 1); } } CB_Cat_Trusted_Utf8(buf, "\"", 1); String *retval = CB_Yield_String(buf); DECREF(buf); return retval; }
String* ORQuery_To_String_IMP(ORQuery *self) { ORQueryIVARS *const ivars = ORQuery_IVARS(self); uint32_t num_kids = Vec_Get_Size(ivars->children); if (!num_kids) { return Str_new_from_trusted_utf8("()", 2); } else { CharBuf *buf = CB_new_from_trusted_utf8("(", 1); uint32_t last_kid = num_kids - 1; for (uint32_t i = 0; i < num_kids; i++) { String *kid_string = Obj_To_String(Vec_Fetch(ivars->children, i)); CB_Cat(buf, kid_string); DECREF(kid_string); if (i == last_kid) { CB_Cat_Trusted_Utf8(buf, ")", 1); } else { CB_Cat_Trusted_Utf8(buf, " OR ", 4); } } String *retval = CB_Yield_String(buf); DECREF(buf); return retval; } }
void ErrMsg_set_with_win_error(const char *fmt, ...) { char *win_error = Err_win_error(); CharBuf *buf = CB_new(0); va_list args; va_start(args, fmt); CB_VCatF(buf, fmt, args); va_end(args); CB_Cat_Trusted_Utf8(buf, ": ", 2); CB_Cat_Utf8(buf, win_error, strlen(win_error)); Err_set_error(Err_new(CB_Yield_String(buf))); DECREF(buf); FREEMEM(win_error); }
static Hash* S_extract_tv_cache(ByteBuf *field_buf) { Hash *tv_cache = Hash_new(0); const char *tv_string = BB_Get_Buf(field_buf); int32_t num_terms = NumUtil_decode_c32(&tv_string); CharBuf *text_buf = CB_new(0); // Read the number of highlightable terms in the field. for (int32_t i = 0; i < num_terms; i++) { size_t overlap = NumUtil_decode_c32(&tv_string); size_t len = NumUtil_decode_c32(&tv_string); // Decompress the term text. CB_Set_Size(text_buf, overlap); CB_Cat_Trusted_Utf8(text_buf, tv_string, len); tv_string += len; // Get positions & offsets string. const char *bookmark_ptr = tv_string; int32_t num_positions = NumUtil_decode_c32(&tv_string); while (num_positions--) { // Leave nums compressed to save a little mem. NumUtil_skip_cint(&tv_string); NumUtil_skip_cint(&tv_string); NumUtil_skip_cint(&tv_string); } len = tv_string - bookmark_ptr; // Store the $text => $posdata pair in the output hash. String *text = CB_To_String(text_buf); Hash_Store(tv_cache, (Obj*)text, (Obj*)BB_new_bytes(bookmark_ptr, len)); DECREF(text); } DECREF(text_buf); return tv_cache; }