/** * search **/ static VALUE t_search(VALUE self, VALUE str) { node root, now, ret; char *text; long i, total_len; long head_i, tail_i; VALUE array; rb_encoding *enc; array = rb_ary_new(); enc = rb_enc_get(str); text = StringValuePtr(str); Data_Get_Struct(self, struct _node, root); now = root; total_len = strlen(text); head_i = -1; tail_i = -1; for(i = 0; i <= total_len; i++) { ret = search_child(now, text[i]); if (ret && i != total_len) { if (head_i == -1) { head_i = i; } if (ret->end_flag) { tail_i = i; } now = ret; } else { if (head_i != -1) { if (tail_i != -1) { rb_funcall( array, rb_intern("push"), 1, add_encode(rb_str_new(&text[head_i], (tail_i - head_i + 1)), enc) ); i = tail_i; tail_i = -1; } else { i = head_i; } head_i = -1; } now = root; } } return array; }
// Wenn ein Kindprozess beendet wird, der einen // wartenden Server Prozess bedient hat, dann // schmeiss den Socket wieder auf die select() // Liste void sig_child(int code) { inetd *inetd; int status = -1; pid_t pid = wait(&status); if( ((inetd = search_child(pid)) != NULL) && (!strcmp(inetd->type,"wait")) ) { FD_SET(inetd->socket, &readfds); accept_connections(); } }
// search node by use character. // if nothing, create new node static node search_child_or_create(node n, char moji) { node child; child = search_child(n, moji); if(!child) { child = initialize_node(moji); add_child(n, child); } return child; }
Node* ChildList::search_child(shingle_const_iterator shingle_it, shingle_const_iterator shingle_end) { assert(shingle_it != shingle_end); auto it = data.begin(); while(it != data.end() && it->token() != *shingle_it) { ++it; } if(it == data.end()) return nullptr; return it->search_child(shingle_it + 1, shingle_end); };
/** * filter_html **/ static VALUE t_filter_html(VALUE self, VALUE str) { node root, now, ret; bool in_tag; char *text; const char* inner_tag; long i, head_i, tail_i, copy_head_i, total_len; VALUE change_str, url_base, word; rb_encoding *enc; change_str = rb_str_new2(EMPTY_STRING); enc = rb_enc_get(str); text = StringValuePtr(str); Data_Get_Struct(self, struct _node, root); url_base = rb_iv_get(self, LINK_URL_VARIABLE); if (url_base == Qnil) { url_base = rb_str_new2(DEAULT_LINK_URL); } now = root; total_len = strlen(text); head_i = -1; tail_i = -1; copy_head_i = 0; in_tag = false; inner_tag = NULL; for(i = 0; i <= total_len; i++) { if (!in_tag && text[i] == BEGIN_TAG) { in_tag = true; if (strncasecmp(&text[i + 1], A_TAG, strlen(A_TAG)) == 0) { inner_tag = A_TAG; } else if (strncasecmp(&text[i + 1], SCRIPT_TAG, strlen(SCRIPT_TAG)) == 0) { inner_tag = SCRIPT_TAG; } else if (strncasecmp(&text[i + 1], PRE_TAG, strlen(PRE_TAG)) == 0) { inner_tag = PRE_TAG; } else if (strncasecmp(&text[i + 1], IFRAME_TAG, strlen(IFRAME_TAG)) == 0) { inner_tag = IFRAME_TAG; } else if (strncasecmp(&text[i + 1], OBJECT_TAG, strlen(OBJECT_TAG)) == 0) { inner_tag = OBJECT_TAG; } continue; } if (in_tag && !inner_tag && text[i] == END_TAG) { in_tag = false; continue; } if (inner_tag && text[i] == BEGIN_TAG) { if (strncasecmp(&text[i + 2], inner_tag, strlen(inner_tag)) == 0) { inner_tag = NULL; continue; } } if (in_tag) { continue; } ret = search_child(now, text[i]); if (ret && i != total_len) { if (head_i == -1) { head_i = i; } if (ret->end_flag) { tail_i = i; } now = ret; } else { if (head_i != -1) { if (tail_i != -1) { if (copy_head_i < head_i) { rb_funcall( change_str, rb_intern("concat"), 1, add_encode(rb_str_new(&text[copy_head_i], (head_i - copy_head_i)), enc) ); } word = rb_str_new(&text[head_i], (tail_i - head_i + 1)); rb_funcall( change_str, rb_intern("concat"), 1, add_encode(rb_funcall(url_base, rb_intern("%"), 1, rb_assoc_new(word, word)), enc) ); i = tail_i; copy_head_i = tail_i + 1; tail_i = -1; } else { i = head_i; } head_i = -1; } now = root; } } if (copy_head_i == 0) { return str; } else { rb_funcall( change_str, rb_intern("concat"), 1, add_encode(rb_str_new(&text[copy_head_i], (total_len - copy_head_i)), enc) ); return change_str; } }
void st_tree_from_line(BooksNode *head, char *line, int eval) { short depth = 0; char move_str[3]; BitBoard bk = FIRST_BK, wh = FIRST_WH; BitBoard rev; int line_len = strlen(line); BooksNode *head_child; while(depth < line_len) { //move_str[0] = 'a' + line[depth] - 'A'; move_str[0] = line[depth]; move_str[1] = line[depth + 1]; move_str[2] = '\0'; /* a1⇒0 など 数値に変換 */ int move = (move_str[0] - 'a') * 8 + move_str[1] - '1'; /* すでに登録されているノードがあるか探す */ head_child = search_child(head, move); /* 新規の場合 */ if(head_child == NULL) { BooksNode *node = (BooksNode *)malloc(sizeof(BooksNode)); node->move = (short )move; if(depth % 4) { rev = GetRev[move](wh, bk); node->bk = bk; node->wh = wh; wh ^= (one << move) | rev; bk ^= rev; } else { rev = GetRev[move](bk, wh); node->bk = bk; node->wh = wh; bk ^= (one << move) | rev; wh ^= rev; } node->eval = eval; node->depth = depth / 2; append(head, node); head = node; } /* 既出の場合 */ else { if(depth % 4) { rev = GetRev[move](wh, bk); wh ^= (one << move) | rev; bk ^= rev; } else { rev = GetRev[move](bk, wh); bk ^= (one << move) | rev; wh ^= rev; } head = head_child; } depth += 2; } }