static tb_charset_ref_t tb_charset_find_by_type(tb_size_t type) { // init iterator tb_iterator_t iterator = tb_iterator_init_mem(g_charsets, tb_arrayn(g_charsets), sizeof(tb_charset_t)); // find it by the binary search tb_size_t itor = tb_binary_find_all_if(&iterator, tb_charset_comp_by_type, (tb_cpointer_t)TB_CHARSET_TYPE(type)); // ok? if (itor != tb_iterator_tail(&iterator)) return (tb_charset_ref_t)tb_iterator_item(&iterator, itor); else return tb_null; }
static tb_charset_ref_t tb_charset_find_by_name(tb_char_t const* name) { // init iterator tb_iterator_t iterator = tb_iterator_init_mem(g_charsets, tb_arrayn(g_charsets), sizeof(tb_charset_t)); // find it by the binary search tb_size_t itor = tb_binary_find_all_if(&iterator, tb_charset_comp_by_name, name); // ok? if (itor != tb_iterator_tail(&iterator)) return (tb_charset_ref_t)tb_iterator_item(&iterator, itor); else return tb_null; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ gb_color_t const gb_color_from_name(tb_char_t const* name) { // check tb_assert_and_check_return_val(name, GB_COLOR_DEFAULT); // init iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_iterator_make_for_mem(&array_iterator, g_named_colors, tb_arrayn(g_named_colors), sizeof(gb_named_color_t)); tb_assert(iterator); // find it by the binary search tb_size_t itor = tb_binary_find_all_if(iterator, gb_named_color_comp, name); // the color gb_named_color_t const* color = (itor != tb_iterator_tail(iterator))? (gb_named_color_t const*)tb_iterator_item(iterator, itor) : tb_null; // ok? return color? color->color : GB_COLOR_DEFAULT; }
static tb_fixed_pool_slot_t* tb_fixed_pool_slot_find(tb_fixed_pool_impl_t* impl, tb_pointer_t data) { // check tb_assert_and_check_return_val(impl && data, tb_null); // init the iterator tb_iterator_t iterator = tb_iterator_init_ptr((tb_pointer_t*)impl->slot_list, impl->slot_count); // find it tb_size_t itor = tb_binary_find_all_if(&iterator, tb_fixed_pool_slot_comp, data); tb_check_return_val(itor != tb_iterator_tail(&iterator), tb_null); // the slot tb_fixed_pool_slot_t* slot = impl->slot_list[itor]; tb_assert_and_check_return_val(slot, tb_null); // check tb_assert_abort(tb_fixed_pool_slot_exists(slot, data)); // ok? return slot; }
static tb_fixed_pool_slot_t* tb_fixed_pool_slot_find(tb_fixed_pool_t* pool, tb_pointer_t data) { // check tb_assert_and_check_return_val(pool && data, tb_null); // make the iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_iterator_make_for_ptr(&array_iterator, (tb_pointer_t*)pool->slot_list, pool->slot_count); tb_assert(iterator); // find it tb_size_t itor = tb_binary_find_all_if(iterator, tb_fixed_pool_slot_comp, data); tb_check_return_val(itor != tb_iterator_tail(iterator), tb_null); // the slot tb_fixed_pool_slot_t* slot = pool->slot_list[itor]; tb_assert_and_check_return_val(slot, tb_null); // check tb_assert(tb_fixed_pool_slot_exists(slot, data)); // ok? return slot; }
tb_bool_t vm86_parser_get_register(tb_char_t const** pp, tb_char_t const* e, tb_uint16_t* r) { // check tb_assert(pp && e && r); // done tb_bool_t ok = tb_false; tb_char_t const* p = *pp; do { // save base tb_char_t const* b = p; // get instruction name tb_char_t name[64] = {0}; while (p < e && tb_isalpha(*p)) p++; tb_check_break(p <= e && p - b < sizeof(name)); tb_memcpy(name, b, p - b); // skip the space while (p < e && tb_isspace(*p)) p++; // the register entry type typedef struct __vm86_register_entry_t { // the register name tb_char_t const* name; // the register index tb_uint8_t index; }vm86_register_entry_t, *vm86_register_entry_ref_t; // the registers static vm86_register_entry_t s_registers[] = { { "ah", VM86_REGISTER_EAX | VM86_REGISTER_AH } , { "al", VM86_REGISTER_EAX | VM86_REGISTER_AL } , { "ax", VM86_REGISTER_EAX | VM86_REGISTER_AX } , { "bh", VM86_REGISTER_EBX | VM86_REGISTER_BH } , { "bl", VM86_REGISTER_EBX | VM86_REGISTER_BL } , { "bx", VM86_REGISTER_EBX | VM86_REGISTER_BX } , { "ch", VM86_REGISTER_ECX | VM86_REGISTER_CH } , { "cl", VM86_REGISTER_ECX | VM86_REGISTER_CL } , { "cx", VM86_REGISTER_ECX | VM86_REGISTER_CX } , { "dh", VM86_REGISTER_EDX | VM86_REGISTER_DH } , { "dl", VM86_REGISTER_EDX | VM86_REGISTER_DL } , { "dx", VM86_REGISTER_EDX | VM86_REGISTER_DX } , { "eax", VM86_REGISTER_EAX } , { "ebp", VM86_REGISTER_EBP } , { "ebx", VM86_REGISTER_EBX } , { "ecx", VM86_REGISTER_ECX } , { "edi", VM86_REGISTER_EDI } , { "edx", VM86_REGISTER_EDX } , { "esi", VM86_REGISTER_ESI } , { "esp", VM86_REGISTER_ESP } }; // init iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_array_iterator_init_mem(&array_iterator, s_registers, tb_arrayn(s_registers), sizeof(vm86_register_entry_t)); // find register by the binary search tb_size_t itor = tb_binary_find_all_if(iterator, vm86_parser_comp_register, name); tb_check_break(itor != tb_iterator_tail(iterator)); // get the register vm86_register_entry_ref_t entry = (vm86_register_entry_ref_t)tb_iterator_item(iterator, itor); tb_assert_and_check_break(entry && (entry->index & VM86_REGISTER_MASK) < VM86_REGISTER_MAXN); // save register *r = entry->index; // trace tb_trace_d("register: %s: %x", name, entry->index); // ok ok = tb_true; } while (0); // update the code pointer if ok if (ok) *pp = p; // ok? return ok; }