void gui_input_delete_end_of_line (const char *args) { char *start; int size_deleted, length_deleted, pos_start; /* make C compiler happy */ (void) args; if (gui_current_window->buffer->input) { start = utf8_add_offset (gui_current_window->buffer->input_buffer, gui_current_window->buffer->input_buffer_pos); pos_start = start - gui_current_window->buffer->input_buffer; size_deleted = strlen (start); length_deleted = utf8_strlen (start); gui_input_clipboard_copy (start, size_deleted); start[0] = '\0'; gui_current_window->buffer->input_buffer_size = strlen (gui_current_window->buffer->input_buffer); gui_current_window->buffer->input_buffer_length = utf8_strlen (gui_current_window->buffer->input_buffer); gui_input_optimize_size (gui_current_window->buffer); gui_completion_stop (gui_current_window->buffer->completion, 1); gui_input_text_changed_modifier_and_signal (gui_current_window->buffer); } }
int gui_completion_nickncmp (const char *base_word, const char *nick, int max) { char *base_word2, *nick2; int case_sensitive, return_cmp; case_sensitive = CONFIG_BOOLEAN(config_completion_nick_case_sensitive); if (!CONFIG_STRING(config_completion_nick_ignore_chars) || !CONFIG_STRING(config_completion_nick_ignore_chars)[0] || !base_word[0] || !nick[0] || gui_completion_nick_has_ignored_chars (base_word)) { return (case_sensitive) ? strncmp (base_word, nick, max) : string_strncasecmp (base_word, nick, max); } base_word2 = gui_completion_nick_strdup_ignore_chars (base_word); nick2 = gui_completion_nick_strdup_ignore_chars (nick); return_cmp = (case_sensitive) ? strncmp (base_word2, nick2, utf8_strlen (base_word2)) : string_strncasecmp (base_word2, nick2, utf8_strlen (base_word2)); free (base_word2); free (nick2); return return_cmp; }
/** * Insert 'ch' at position 'pos' * * Returns 1 if the line needs to be refreshed, 2 if not * and 0 if nothing was inserted (no room) */ static int insert_char(struct current *current, int pos, int ch) { char buf[3]; int n = utf8_getchars(buf, ch); if (has_room(current, n) && pos >= 0 && pos <= current->chars) { int p1, p2; int ret = 1; p1 = utf8_index(current->buf, pos); p2 = p1 + n; #ifdef USE_TERMIOS /* optimise the case where adding a single char to the end and no scrolling is needed */ if (current->pos == pos && current->chars == pos) { if (ch >= ' ' && utf8_strlen(current->prompt, -1) + utf8_strlen(current->buf, current->len) < current->cols - 1) { IGNORE_RC(write(current->fd, buf, n)); ret = 2; } } #endif memmove(current->buf + p2, current->buf + p1, current->len - p1); memcpy(current->buf + p1, buf, n); current->len += n; current->chars++; if (current->pos >= pos) { current->pos++; } return ret; } return 0; }
/** * Removes the char at 'pos'. * * Returns 1 if the line needs to be refreshed, 2 if not * and 0 if nothing was removed */ static int remove_char(struct current *current, int pos) { if (pos >= 0 && pos < current->chars) { int p1, p2; int ret = 1; p1 = utf8_index(current->buf, pos); p2 = p1 + utf8_index(current->buf + p1, 1); #ifdef USE_TERMIOS /* optimise remove char in the case of removing the last char */ if (current->pos == pos + 1 && current->pos == current->chars) { if (current->buf[pos] >= ' ' && utf8_strlen(current->prompt, -1) + utf8_strlen(current->buf, current->len) < current->cols - 1) { ret = 2; fd_printf(current->fd, "\b \b"); } } #endif /* Move the null char too */ memmove(current->buf + p1, current->buf + p2, current->len - p2 + 1); current->len -= (p2 - p1); current->chars--; if (current->pos > pos) { current->pos--; } return ret; } return 0; }
int utf8_strlen_screen (const char *string) { int length, num_char; wchar_t *wstring; if (!string) return 0; if (!local_utf8) return utf8_strlen (string); num_char = mbstowcs (NULL, string, 0) + 1; wstring = malloc ((num_char + 1) * sizeof (wstring[0])); if (!wstring) return utf8_strlen (string); if (mbstowcs (wstring, string, num_char) == (size_t)(-1)) { free (wstring); return utf8_strlen (string); } length = wcswidth (wstring, num_char); free (wstring); return length; }
void gui_completion_list_add (struct t_gui_completion *completion, const char *word, int nick_completion, const char *where) { char buffer[512]; if (!word || !word[0]) return; if (!completion->base_word || !completion->base_word[0] || (nick_completion && (gui_completion_nickncmp (completion->base_word, word, utf8_strlen (completion->base_word)) == 0)) || (!nick_completion && (string_strncasecmp (completion->base_word, word, utf8_strlen (completion->base_word)) == 0))) { if (nick_completion && (completion->base_word_pos == 0)) { snprintf (buffer, sizeof (buffer), "%s%s", word, CONFIG_STRING(config_completion_nick_completer)); weelist_add (completion->completion_list, buffer, where, (nick_completion) ? (void *)1 : (void *)0); } else { weelist_add (completion->completion_list, word, where, (nick_completion) ? (void *)1 : (void *)0); } } }
void ST7920_Lite_Status_Screen::draw_status_message() { const char *str = ui.status_message; set_ddram_address(DDRAM_LINE_4); begin_data(); #if ENABLED(STATUS_MESSAGE_SCROLLING) uint8_t slen = utf8_strlen(str); if (slen <= LCD_WIDTH) { // String fits the LCD, so just print it write_str(str); for (; slen < LCD_WIDTH; ++slen) write_byte(' '); } else { // String is larger than the available space in screen. // Get a pointer to the next valid UTF8 character const char *stat = str + ui.status_scroll_offset; // Get the string remaining length const uint8_t rlen = utf8_strlen(stat); // If we have enough characters to display if (rlen >= LCD_WIDTH) { // The remaining string fills the screen - Print it write_str(stat, LCD_WIDTH); } else { // The remaining string does not completely fill the screen write_str(stat); // The string leaves space uint8_t chars = LCD_WIDTH - rlen; // Amount of space left in characters write_byte('.'); // Always at 1+ spaces left, draw a dot if (--chars) { // Draw a second dot if there's space write_byte('.'); if (--chars) write_str(str, chars); // Print a second copy of the message } } // Adjust by complete UTF8 characters if (ui.status_scroll_offset < slen) { ui.status_scroll_offset++; while (!START_OF_UTF8_CHAR(str[ui.status_scroll_offset])) ui.status_scroll_offset++; } else ui.status_scroll_offset = 0; } #else uint8_t slen = utf8_strlen(str); write_str(str, LCD_WIDTH); for (; slen < LCD_WIDTH; ++slen) write_byte(' '); #endif }
// ---------------------------------------------------------------------------- void text_buffer_add_text( text_buffer_t * self, vec2 * pen, markup_t * markup, const char * text, size_t length ) { font_manager_t * manager = self->manager; size_t i; const char * prev_character = NULL; if( markup == NULL ) { return; } if( !markup->font ) { markup->font = font_manager_get_from_markup( manager, markup ); if( ! markup->font ) { fprintf( stderr, "Houston, we've got a problem !\n" ); exit( EXIT_FAILURE ); } } if( length == 0 ) { length = utf8_strlen(text); } if( vertex_buffer_size( self->buffer ) == 0 ) { self->origin = *pen; self->line_left = pen->x; self->bounds.left = pen->x; self->bounds.top = pen->y; } else { if (pen->x < self->origin.x) { self->origin.x = pen->x; } if (pen->y != self->last_pen_y) { text_buffer_finish_line(self, pen, false); } } for( i = 0; utf8_strlen( text + i ) && length; i += utf8_surrogate_len( text + i ) ) { text_buffer_add_char( self, pen, markup, text + i, prev_character ); prev_character = text + i; length--; } self->last_pen_y = pen->y; }
void gui_completion_list_add (struct t_gui_completion *completion, const char *word, int nick_completion, const char *where) { struct t_gui_completion_word *completion_word; char buffer[512]; int index; if (!word || !word[0]) return; if (!completion->base_word || !completion->base_word[0] || (nick_completion && (gui_completion_nickncmp (completion->base_word, word, utf8_strlen (completion->base_word)) == 0)) || (!nick_completion && (string_strncasecmp (completion->base_word, word, utf8_strlen (completion->base_word)) == 0))) { completion_word = malloc (sizeof (*completion_word)); if (completion_word) { completion_word->nick_completion = nick_completion; completion_word->count = 0; index = -1; if (strcmp (where, WEECHAT_LIST_POS_BEGINNING) == 0) { completion->list->sorted = 0; index = 0; } else if (strcmp (where, WEECHAT_LIST_POS_END) == 0) { completion->list->sorted = 0; index = -1; } if (nick_completion && (completion->base_word_pos == 0)) { snprintf (buffer, sizeof (buffer), "%s%s", word, CONFIG_STRING(config_completion_nick_completer)); completion_word->word = strdup (buffer); arraylist_insert (completion->list, index, completion_word); completion->add_space = 0; } else { completion_word->word = strdup (word); arraylist_insert (completion->list, index, completion_word); } } } }
void gui_input_history_global_next () { if ((gui_current_window->buffer->input) && (gui_current_window->buffer->text_search == GUI_TEXT_SEARCH_DISABLED)) { if (history_global_ptr) { history_global_ptr = history_global_ptr->prev_history; if (history_global_ptr) { gui_current_window->buffer->input_buffer_size = strlen (history_global_ptr->text); gui_current_window->buffer->input_buffer_length = utf8_strlen (history_global_ptr->text); } else { gui_current_window->buffer->input_buffer[0] = '\0'; gui_current_window->buffer->input_buffer_size = 0; gui_current_window->buffer->input_buffer_length = 0; } gui_input_optimize_size (gui_current_window->buffer); gui_current_window->buffer->input_buffer_pos = gui_current_window->buffer->input_buffer_length; gui_current_window->buffer->input_buffer_1st_display = 0; if (history_global_ptr) { strcpy (gui_current_window->buffer->input_buffer, history_global_ptr->text); } gui_input_text_changed_modifier_and_signal (gui_current_window->buffer); } } }
result_t Regex::test(const char *str, bool &retVal) { int32_t rc = 0; int32_t ovector[RE_SIZE]; int32_t len = (int32_t) qstrlen(str); const char *end = str + len; if (m_bGlobal) { int32_t n = m_nlastIndex; while (n > 0 && utf8_getchar(str, end)) n--; } if (*str) { len = (int32_t) qstrlen(str); rc = pcre_exec(m_re, NULL, str, len, 0, 0, ovector, RE_SIZE); if (rc < 0) { rc = 0; m_nlastIndex = 0; } } if (rc) { retVal = true; if (m_bGlobal) m_nlastIndex += utf8_strlen(str, ovector[2 * rc - 1]); } return 0; }
void gui_input_history_next () { if (gui_current_window->buffer->input) { if (gui_current_window->buffer->text_search == GUI_TEXT_SEARCH_DISABLED) { if (gui_current_window->buffer->ptr_history) { gui_current_window->buffer->ptr_history = gui_current_window->buffer->ptr_history->prev_history; if (gui_current_window->buffer->ptr_history) { gui_current_window->buffer->input_buffer_size = strlen (gui_current_window->buffer->ptr_history->text); gui_current_window->buffer->input_buffer_length = utf8_strlen (gui_current_window->buffer->ptr_history->text); } else { gui_current_window->buffer->input_buffer[0] = '\0'; gui_current_window->buffer->input_buffer_size = 0; gui_current_window->buffer->input_buffer_length = 0; } gui_input_optimize_size (gui_current_window->buffer); gui_current_window->buffer->input_buffer_pos = gui_current_window->buffer->input_buffer_length; gui_current_window->buffer->input_buffer_1st_display = 0; if (gui_current_window->buffer->ptr_history) { strcpy (gui_current_window->buffer->input_buffer, gui_current_window->buffer->ptr_history->text); } } else { /* add line to history then clear input */ if (gui_current_window->buffer->input_buffer_size > 0) { gui_current_window->buffer->input_buffer[gui_current_window->buffer->input_buffer_size] = '\0'; gui_history_buffer_add (gui_current_window->buffer, gui_current_window->buffer->input_buffer); gui_history_global_add (gui_current_window->buffer->input_buffer); gui_current_window->buffer->input_buffer[0] = '\0'; gui_current_window->buffer->input_buffer_size = 0; gui_current_window->buffer->input_buffer_length = 0; gui_current_window->buffer->input_buffer_pos = 0; gui_current_window->buffer->input_buffer_1st_display = 0; gui_input_optimize_size (gui_current_window->buffer); } } gui_input_text_changed_modifier_and_signal (gui_current_window->buffer); } else { /* search forward in buffer history */ gui_current_window->buffer->text_search = GUI_TEXT_SEARCH_FORWARD; (void) gui_window_search_text (gui_current_window); } } }
static void set_current(struct current *current, const char *str) { strncpy(current->buf, str, current->bufmax); current->buf[current->bufmax - 1] = 0; current->len = strlen(current->buf); current->pos = current->chars = utf8_strlen(current->buf, current->len); }
/** * Returns length of given string for parser's encoding */ static size_t acmp_strlen(ACMP *parser, const char *str) { #ifdef ACMP_USE_UTF8 return (parser->is_utf8 == 0) ? strlen(str) : utf8_strlen(str); #else return strlen(str); #endif }
int utf8_strlen_screen (const char *string) { int length, num_char; wchar_t *alloc_wstring, *ptr_wstring, wstring[4+2]; if (!string || !string[0]) return 0; if (!local_utf8) return utf8_strlen (string); alloc_wstring = NULL; if (!string[1] || !string[2] || !string[3] || !string[4]) { /* optimization for max 4 chars: no malloc */ num_char = 4 + 1; ptr_wstring = wstring; } else { num_char = mbstowcs (NULL, string, 0) + 1; alloc_wstring = malloc ((num_char + 1) * sizeof (alloc_wstring[0])); if (!alloc_wstring) return utf8_strlen (string); ptr_wstring = alloc_wstring; } if (mbstowcs (ptr_wstring, string, num_char) != (size_t)(-1)) { length = wcswidth (ptr_wstring, num_char); /* * ensure the size is always >= 1, to prevent any display bug * (for example size of UTF-8 char U+26C4 is -1, why? mystery...) */ if (length < 1) length = 1; } else length = utf8_strlen (string); if (alloc_wstring) free (alloc_wstring); return length; }
void utf8_strcat_utf8_string(utf8_string *work, const utf8_string *in) { const int len = utf8_strlen(in); int loop; for(loop=0; loop<len; loop++) add_utf8_to_utf8_string(work, in -> string[loop]); }
size_t linkage_get_word_char_end(const Linkage linkage, WordIdx w) { if (linkage->num_words <= w) return 0; /* bounds-check */ int pos = linkage->wg_path_display[w]->end - linkage->sent->orig_sentence; char *sentchunk = alloca(pos+1); strncpy(sentchunk, linkage->sent->orig_sentence, pos); sentchunk[pos] = '\0'; return utf8_strlen(sentchunk); }
/* добавить текст в буффер */ static void bufferAppend(char *data, int len, FB2Content *fb) /*, int remove_spaces)*/ { char **buffer; size_t *buffer_size; int *current_index; /*int new_len;*/ if (!data) return; /* добавляем в текущий буффер */ if (fb->current_buffer == DESCRIPTION_BUFFER) { buffer = &(fb->description); buffer_size = &(fb->description_buffer_size); current_index = &(fb->description_current_index); } else if (fb->current_buffer == TEXT_BUFFER) { buffer = &(fb->text); buffer_size = &(fb->text_buffer_size); current_index = &(fb->text_current_index); } else if (fb->current_buffer == BINARY_BUFFER) { buffer = &(fb->binaries[fb->num_binaries]->buffer); buffer_size = &(fb->binaries[fb->num_binaries]->buffer_size); current_index = &(fb->binaries[fb->num_binaries]->current_index); } /* remove spaces */ /* if (remove_spaces) { new_len = removeSpaces(data, len); len = new_len; } */ /* realloc buffer */ if (*current_index+len >= *buffer_size) { while (1) { *buffer_size = *buffer_size*2; *buffer = (char *) realloc(*buffer, *buffer_size); if (*current_index+len < *buffer_size) { break; } } } strncpy((*buffer) + *current_index, data, len); *current_index += len; if (fb->current_buffer == TEXT_BUFFER) { int u_len; u_len = utf8_strlen(data); fb->utf8_current_index += u_len; } return; }
static int completeLine(struct current *current) { linenoiseCompletions lc = { 0, NULL }; int c = 0; completionCallback(current->buf,&lc,completionUserdata); if (lc.len == 0) { beep(); } else { size_t stop = 0, i = 0; while(!stop) { /* Show completion or original buffer */ if (i < lc.len) { struct current tmp = *current; tmp.buf = lc.cvec[i]; tmp.pos = tmp.len = strlen(tmp.buf); tmp.chars = utf8_strlen(tmp.buf, tmp.len); refreshLine(current->prompt, &tmp); } else { refreshLine(current->prompt, current); } c = fd_read(current); if (c == -1) { break; } switch(c) { case '\t': /* tab */ i = (i+1) % (lc.len+1); if (i == lc.len) beep(); break; case 27: /* escape */ /* Re-show original buffer */ if (i < lc.len) { refreshLine(current->prompt, current); } stop = 1; break; default: /* Update buffer and return */ if (i < lc.len) { set_current(current,lc.cvec[i]); } stop = 1; break; } } } freeCompletions(&lc); return c; /* Return last read character */ }
static int op_strlen(int argc, char **argv) { char *opname = *argv++; argc--; if (argc != 1) { dico_log(L_ERR, 0, "%s requires one argument", opname); return 1; } printf("%lu\n", (unsigned long) utf8_strlen(argv[0])); return 0; }
int System(char *command) /* command is a UTF-8 string */ { STARTUPINFOW sinfo; PROCESS_INFORMATION pinfo; int shell_rval; size_t len; wchar_t *wcmd; memset(&sinfo, 0, sizeof(sinfo)); sinfo.cb = sizeof(sinfo); len = utf8_strlen(command, strlen(command)); wcmd = PL_malloc((len+1)*sizeof(wchar_t)); utf8towcs(wcmd, command); if ( CreateProcessW(NULL, /* module */ wcmd, /* command line */ NULL, /* Security stuff */ NULL, /* Thread security stuff */ FALSE, /* Inherit handles */ CREATE_NO_WINDOW, /* flags */ NULL, /* environment */ NULL, /* CWD */ &sinfo, /* startup info */ &pinfo) ) /* process into */ { BOOL rval; DWORD code; CloseHandle(pinfo.hThread); /* don't need this */ PL_free(wcmd); do { MSG msg; if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) { TranslateMessage(&msg); DispatchMessage(&msg); } else Sleep(50); rval = GetExitCodeProcess(pinfo.hProcess, &code); } while(rval == TRUE && code == STILL_ACTIVE); shell_rval = (rval == TRUE ? code : -1); CloseHandle(pinfo.hProcess); } else { PL_free(wcmd); return shell_rval = -1; } return shell_rval; }
static unsigned int text_width(char* str) { if (iconvW == NULL) iconvW = iconv_open("ucs-4be", "utf-8"); if (iconvW == (iconv_t) -1) { return utf8_strlen(str); } else { size_t len = strlen(str); size_t charlen = utf8_strlen(str); unsigned *wmessage; size_t wlen = (len + 1) * sizeof (unsigned); wmessage = (unsigned *) fcitx_malloc0 ((len + 1) * sizeof (unsigned)); char *inp = str; char *outp = (char*) wmessage; iconv(iconvW, &inp, &len, &outp, &wlen); int i = 0; int width = 0; for (i = 0; i < charlen; i ++) { if (is_double_width(wmessage[i])) width += 2; else width += 1; } free(wmessage); return width; } }
/* TODO: parse overlining & underlining as distinct sections. */ static void findRstTags (void) { vString *name = vStringNew (); fpos_t filepos; const unsigned char *line; memset(&filepos, 0, sizeof(fpos_t)); memset(kindchars, 0, sizeof kindchars); nestingLevels = nestingLevelsNew(); while ((line = readLineFromInputFile ()) != NULL) { int line_len = strlen((const char*) line); int name_len_bytes = vStringLength(name); int name_len = utf8_strlen(vStringValue(name), name_len_bytes); /* if the name doesn't look like UTF-8, assume one-byte charset */ if (name_len < 0) name_len = name_len_bytes; /* underlines must be the same length or more */ if (line_len >= name_len && name_len > 0 && ispunct(line[0]) && issame((const char*) line)) { char c = line[0]; int kind = get_kind(c); if (kind >= 0) { makeRstTag(name, kind, filepos); continue; } } vStringClear (name); if (!isspace(*line)) { vStringCatS(name, (const char*)line); filepos = getInputFilePosition(); } vStringTerminate(name); } vStringDelete (name); nestingLevelsFree(nestingLevels); }
result_t Regex::exec(const char *str, v8::Local<v8::Array> &retVal) { int32_t rc = 0; int32_t ovector[RE_SIZE]; int32_t len = (int32_t) qstrlen(str); const char *end = str + len; int32_t i; if (m_bGlobal) { int32_t n = m_nlastIndex; while (n > 0 && utf8_getchar(str, end)) n--; } if (*str) { len = (int32_t) qstrlen(str); rc = pcre_exec(m_re, NULL, str, len, 0, 0, ovector, RE_SIZE); if (rc < 0) { rc = 0; m_nlastIndex = 0; } } if (rc) { Isolate* isolate = Isolate::now(); retVal = v8::Array::New(isolate->m_isolate, rc); for (i = 0; i < rc; i++) retVal->Set(i, v8::String::NewFromUtf8(isolate->m_isolate, str + ovector[2 * i], v8::String::kNormalString, ovector[2 * i + 1] - ovector[2 * i])); if (m_bGlobal) m_nlastIndex += utf8_strlen(str, ovector[2 * rc - 1]); } return rc ? 0 : CALL_RETURN_NULL; }
TEST(CoreUtf8, Size) { /* char size */ LONGS_EQUAL(0, utf8_char_size (NULL)); LONGS_EQUAL(1, utf8_char_size ("")); LONGS_EQUAL(1, utf8_char_size ("A")); LONGS_EQUAL(2, utf8_char_size ("ë")); LONGS_EQUAL(3, utf8_char_size ("€")); LONGS_EQUAL(3, utf8_char_size (cjk_yellow)); LONGS_EQUAL(4, utf8_char_size (han_char)); /* char size on screen */ LONGS_EQUAL(0, utf8_char_size_screen (NULL)); LONGS_EQUAL(0, utf8_char_size_screen ("")); LONGS_EQUAL(1, utf8_char_size_screen ("A")); LONGS_EQUAL(1, utf8_char_size_screen ("ë")); LONGS_EQUAL(1, utf8_char_size_screen ("€")); LONGS_EQUAL(2, utf8_char_size_screen (cjk_yellow)); /* length of string (in chars) */ LONGS_EQUAL(0, utf8_strlen (NULL)); LONGS_EQUAL(0, utf8_strlen ("")); LONGS_EQUAL(1, utf8_strlen ("A")); LONGS_EQUAL(1, utf8_strlen ("ë")); LONGS_EQUAL(1, utf8_strlen ("€")); LONGS_EQUAL(1, utf8_strlen (cjk_yellow)); LONGS_EQUAL(1, utf8_strlen (han_char)); /* length of string (in chars, for max N bytes) */ LONGS_EQUAL(0, utf8_strnlen (NULL, 0)); LONGS_EQUAL(0, utf8_strnlen ("", 0)); LONGS_EQUAL(1, utf8_strnlen ("AZ", 1)); LONGS_EQUAL(1, utf8_strnlen ("ëZ", 2)); LONGS_EQUAL(1, utf8_strnlen ("€Z", 3)); LONGS_EQUAL(1, utf8_strnlen (han_char_z, 4)); /* length of string on screen (in chars) */ LONGS_EQUAL(0, utf8_strlen_screen (NULL)); LONGS_EQUAL(0, utf8_strlen_screen ("")); LONGS_EQUAL(1, utf8_strlen_screen ("A")); LONGS_EQUAL(1, utf8_strlen_screen ("ë")); LONGS_EQUAL(1, utf8_strlen_screen ("€")); LONGS_EQUAL(1, utf8_strlen_screen ("\x7f")); LONGS_EQUAL(2, utf8_strlen_screen (cjk_yellow)); }
void gui_input_replace_input (struct t_gui_buffer *buffer, const char *new_input) { int size, length; size = strlen (new_input); length = utf8_strlen (new_input); /* compute new buffer size */ buffer->input_buffer_size = size; buffer->input_buffer_length = length; gui_input_optimize_size (buffer); /* copy new string to input */ strcpy (buffer->input_buffer, new_input); /* move cursor to the end of new input if it is now after the end */ if (buffer->input_buffer_pos > buffer->input_buffer_length) buffer->input_buffer_pos = buffer->input_buffer_length; }
static KMETHOD String_search(KonohaContext *kctx, KonohaStack *sfp) { kRegExp *re = sfp[1].asRegExp; intptr_t loc = -1; if(!IS_NULL(re) && S_size(re->pattern) > 0) { kregmatch_t pmatch[2]; // modified by @utrhira const char *str = S_text(sfp[0].asString); // necessary int res = pcre_regexec(kctx, re->reg, str, 1, pmatch, re->eflags); if(res == 0) { loc = pmatch[0].rm_so; if(loc != -1 && !kString_is(ASCII, sfp[0].asString)) { loc = utf8_strlen(str, loc); } } else { //TODO //LOG_regex(kctx, sfp, res, re, str); } } KReturnUnboxValue(loc); }
TEST(Utf8, Size) { /* char size */ LONGS_EQUAL(0, utf8_char_size (NULL)); LONGS_EQUAL(1, utf8_char_size ("")); LONGS_EQUAL(1, utf8_char_size ("A")); LONGS_EQUAL(2, utf8_char_size ("ë")); LONGS_EQUAL(3, utf8_char_size ("€")); LONGS_EQUAL(4, utf8_char_size (han_char)); /* char size on screen */ LONGS_EQUAL(0, utf8_char_size_screen (NULL)); LONGS_EQUAL(0, utf8_char_size_screen ("")); LONGS_EQUAL(1, utf8_char_size_screen ("A")); LONGS_EQUAL(1, utf8_char_size_screen ("ë")); LONGS_EQUAL(1, utf8_char_size_screen ("€")); /* this test does not work on Ubuntu Precise: it returns 2 instead of 1 */ /*LONGS_EQUAL(1, utf8_char_size_screen (han_char));*/ /* length of string (in chars) */ LONGS_EQUAL(0, utf8_strlen (NULL)); LONGS_EQUAL(0, utf8_strlen ("")); LONGS_EQUAL(1, utf8_strlen ("A")); LONGS_EQUAL(1, utf8_strlen ("ë")); LONGS_EQUAL(1, utf8_strlen ("€")); LONGS_EQUAL(1, utf8_strlen (han_char)); /* length of string (in chars, for max N bytes) */ LONGS_EQUAL(0, utf8_strnlen (NULL, 0)); LONGS_EQUAL(0, utf8_strnlen ("", 0)); LONGS_EQUAL(1, utf8_strnlen ("AZ", 1)); LONGS_EQUAL(1, utf8_strnlen ("ëZ", 2)); LONGS_EQUAL(1, utf8_strnlen ("€Z", 3)); LONGS_EQUAL(1, utf8_strnlen (han_char_z, 4)); /* length of string on screen (in chars) */ LONGS_EQUAL(0, utf8_strlen_screen (NULL)); LONGS_EQUAL(0, utf8_strlen_screen ("")); LONGS_EQUAL(1, utf8_strlen_screen ("A")); LONGS_EQUAL(1, utf8_strlen_screen ("ë")); LONGS_EQUAL(1, utf8_strlen_screen ("€")); /* this test does not work on Ubuntu Precise: it returns 2 instead of 1 */ /*LONGS_EQUAL(1, utf8_strlen_screen (han_char));*/ LONGS_EQUAL(1, utf8_strlen_screen ("\x7f")); }
int gui_completion_nickncmp (const char *base_word, const char *nick, int max) { char *base_word2, *nick2; int return_cmp; if (!CONFIG_STRING(config_completion_nick_ignore_chars) || !CONFIG_STRING(config_completion_nick_ignore_chars)[0] || !base_word || !nick || !base_word[0] || !nick[0] || gui_completion_nick_has_ignored_chars (base_word)) return string_strncasecmp (base_word, nick, max); base_word2 = gui_completion_nick_strdup_ignore_chars (base_word); nick2 = gui_completion_nick_strdup_ignore_chars (nick); return_cmp = string_strncasecmp (base_word2, nick2, utf8_strlen (base_word2)); free (base_word2); free (nick2); return return_cmp; }
int gui_input_insert_string (struct t_gui_buffer *buffer, const char *string, int pos) { int pos_start, size, length; char *ptr_start; if (buffer->input) { if (pos == -1) pos = buffer->input_buffer_pos; size = strlen (string); length = utf8_strlen (string); /* increase buffer size */ buffer->input_buffer_size += size; buffer->input_buffer_length += length; gui_input_optimize_size (buffer); buffer->input_buffer[buffer->input_buffer_size] = '\0'; /* move end of string to the right */ ptr_start = utf8_add_offset (buffer->input_buffer, pos); pos_start = ptr_start - buffer->input_buffer; memmove (ptr_start + size, ptr_start, strlen (ptr_start)); /* insert new string */ ptr_start = utf8_add_offset (buffer->input_buffer, pos); pos_start = ptr_start - buffer->input_buffer; strncpy (ptr_start, string, size); buffer->input_buffer_pos += length; return length; } return 0; }