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_str_vnewf(char *pattern, va_list args) { CharBuf *buf = CB_new(strlen(pattern) + 10); CB_VCatF(buf, pattern, args); String *message = CB_Yield_String(buf); DECREF(buf); return message; }
String* TestUtils_random_string(size_t length) { CharBuf *buf = CB_new(length); while (length--) { CB_Cat_Char(buf, S_random_code_point()); } String *string = CB_Yield_String(buf); DECREF(buf); return string; }
String* Str_newf(const char *pattern, ...) { CharBuf *buf = CB_new(strlen(pattern)); va_list args; va_start(args, pattern); CB_VCatF(buf, pattern, args); va_end(args); String *self = CB_Yield_String(buf); DECREF(buf); return self; }
void ErrMsg_set(const char *fmt, ...) { CharBuf *buf = CB_new(0); va_list args; va_start(args, fmt); CB_VCatF(buf, fmt, args); va_end(args); Err_set_error(Err_new(CB_Yield_String(buf))); DECREF(buf); }
String* Highlighter_Highlight_IMP(Highlighter *self, String *text) { HighlighterIVARS *const ivars = Highlighter_IVARS(self); size_t size = Str_Get_Size(text) + Str_Get_Size(ivars->pre_tag) + Str_Get_Size(ivars->post_tag); CharBuf *buf = CB_new(size); CB_Cat(buf, ivars->pre_tag); CB_Cat(buf, text); CB_Cat(buf, ivars->post_tag); String *retval = CB_Yield_String(buf); DECREF(buf); return retval; }
String* Method_Host_Name_IMP(Method *self) { StringIterator *iter = StrIter_new(self->name, 0); CharBuf *charbuf = CB_new(Str_Get_Size(self->name)); int32_t code_point; while (STR_OOB != (code_point = StrIter_Next(iter))) { if (code_point != '_') { CB_Cat_Char(charbuf, code_point); } } String *host_name = CB_Yield_String(charbuf); DECREF(charbuf); DECREF(iter); return host_name; }
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 String* S_vmake_mess(const char *file, int line, const char *func, const char *pattern, va_list args) { size_t guess_len = strlen(file) + (func ? strlen(func) : 0) + strlen(pattern) + 30; CharBuf *buf = CB_new(guess_len); CB_VCatF(buf, pattern, args); if (func != NULL) { CB_catf(buf, "\n\t%s at %s line %i32\n", func, file, (int32_t)line); } else { CB_catf(buf, "\n\t%s line %i32\n", file, (int32_t)line); } String *message = CB_Yield_String(buf); DECREF(buf); return message; }
void Err_Add_Frame_IMP(Err *self, const char *file, int line, const char *func) { CharBuf *buf = CB_new(0); CB_Cat(buf, self->mess); if (!Str_Ends_With_Utf8(self->mess, "\n", 1)) { CB_Cat_Char(buf, '\n'); } if (func != NULL) { CB_catf(buf, "\t%s at %s line %i32\n", func, file, (int32_t)line); } else { CB_catf(buf, "\tat %s line %i32\n", file, (int32_t)line); } DECREF(self->mess); self->mess = CB_Yield_String(buf); DECREF(buf); }
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; } }
String* Highlighter_Highlight_Excerpt_IMP(Highlighter *self, VArray *spans, String *raw_excerpt, int32_t top) { int32_t hl_start = 0; int32_t hl_end = 0; StringIterator *iter = Str_Top(raw_excerpt); StringIterator *temp = Str_Top(raw_excerpt); CharBuf *buf = CB_new(Str_Get_Size(raw_excerpt) + 32); CharBuf *encode_buf = NULL; int32_t raw_excerpt_end = top + Str_Length(raw_excerpt); for (uint32_t i = 0, max = VA_Get_Size(spans); i < max; i++) { Span *span = (Span*)VA_Fetch(spans, i); int32_t offset = Span_Get_Offset(span); if (offset < top) { continue; } else if (offset >= raw_excerpt_end) { break; } else { int32_t relative_start = offset - top; int32_t relative_end = relative_start + Span_Get_Length(span); if (relative_start <= hl_end) { if (relative_end > hl_end) { hl_end = relative_end; } } else { if (hl_start < hl_end) { // Highlight previous section int32_t highlighted_len = hl_end - hl_start; StrIter_Assign(temp, iter); StrIter_Advance(iter, highlighted_len); String *to_cat = StrIter_substring(temp, iter); String *encoded = S_do_encode(self, to_cat, &encode_buf); String *hl_frag = Highlighter_Highlight(self, encoded); CB_Cat(buf, hl_frag); DECREF(hl_frag); DECREF(encoded); DECREF(to_cat); } int32_t non_highlighted_len = relative_start - hl_end; StrIter_Assign(temp, iter); StrIter_Advance(iter, non_highlighted_len); String *to_cat = StrIter_substring(temp, iter); String *encoded = S_do_encode(self, to_cat, &encode_buf); CB_Cat(buf, (String*)encoded); DECREF(encoded); DECREF(to_cat); hl_start = relative_start; hl_end = relative_end; } } } if (hl_start < hl_end) { // Highlight final section int32_t highlighted_len = hl_end - hl_start; StrIter_Assign(temp, iter); StrIter_Advance(iter, highlighted_len); String *to_cat = StrIter_substring(temp, iter); String *encoded = S_do_encode(self, to_cat, &encode_buf); String *hl_frag = Highlighter_Highlight(self, encoded); CB_Cat(buf, hl_frag); DECREF(hl_frag); DECREF(encoded); DECREF(to_cat); } // Last text, beyond last highlight span. if (StrIter_Has_Next(iter)) { String *to_cat = StrIter_substring(iter, NULL); String *encoded = S_do_encode(self, to_cat, &encode_buf); CB_Cat(buf, encoded); DECREF(encoded); DECREF(to_cat); } String *highlighted = CB_Yield_String(buf); DECREF(encode_buf); DECREF(buf); DECREF(temp); DECREF(iter); return highlighted; }
String* Highlighter_Raw_Excerpt_IMP(Highlighter *self, String *field_val, int32_t *start_ptr, HeatMap *heat_map) { HighlighterIVARS *const ivars = Highlighter_IVARS(self); // Find start of excerpt. StringIterator *top = Str_Top(field_val); int32_t best_location = S_hottest(heat_map); int32_t start; uint32_t max_skip; if ((uint32_t)best_location <= ivars->slop) { // If the beginning of the string falls within the window centered // around the hottest point in the field, start the fragment at the // beginning. start = 0; max_skip = best_location; } else { start = best_location - ivars->slop; max_skip = ivars->slop; StrIter_Advance(top, start); } uint32_t num_skipped; bool found_starting_edge = S_find_starting_boundary(top, max_skip, &num_skipped); start += num_skipped; // Find end of excerpt. StringIterator *tail = StrIter_Clone(top); uint32_t max_len = ivars->excerpt_length; if (!found_starting_edge) { // Leave space for starting ellipsis and space character. max_len -= 2; } bool found_ending_edge = true; uint32_t excerpt_len = StrIter_Advance(tail, max_len); // Skip up to slop code points but keep at least max_len - slop. if (excerpt_len > max_len - ivars->slop) { max_skip = excerpt_len - (max_len - ivars->slop); found_ending_edge = S_find_ending_boundary(tail, max_skip, &num_skipped); if (num_skipped >= excerpt_len) { excerpt_len = 0; } else { excerpt_len -= num_skipped; } } // Extract excerpt. String *raw_excerpt; if (!excerpt_len) { raw_excerpt = Str_new_from_trusted_utf8("", 0); } else { String *substring = StrIter_substring(top, tail); CharBuf *buf = CB_new(Str_Get_Size(substring) + 8); // If not starting on a sentence boundary, prepend an ellipsis. if (!found_starting_edge) { CB_Cat_Char(buf, ELLIPSIS_CODE_POINT); CB_Cat_Char(buf, ' '); start -= 2; } CB_Cat(buf, substring); // If not ending on a sentence boundary, append an ellipsis. if (!found_ending_edge) { CB_Cat_Char(buf, ELLIPSIS_CODE_POINT); } raw_excerpt = CB_Yield_String(buf); DECREF(buf); DECREF(substring); } *start_ptr = start; DECREF(top); DECREF(tail); return raw_excerpt; }