char* idris_utf8_rev(char* s, char* result) { strcpy(result, s); char* end = result; while(*end) { end = reverse_char(end); } reverse_range(result, end-1); return result; }
int main() { char * my_string = new char[5]; //my_string[0] = '5'; my_string[1] = 'l'; my_string[2] = 'u'; my_string[3] = 'c'; my_string[4] = 'k'; my_string[0] = 5; my_string[1] = 'l'; my_string[2] = 'u'; my_string[3] = 'c'; my_string[4] = 'k'; char * copy_string = new char[1]; int compare_length = 10; //char compare_string[compare_length] = {'l', 'u', 'c', 'k', 'y'}; char * compare_string = new char[compare_length]; compare_string[0] = compare_length; compare_string[1] = 'l'; compare_string[2] = 'a'; compare_string[3] = 'c'; compare_string[4] = 'k'; compare_string[5] = 'y'; // std::cout << "compare_string in main: "; //output(compare_string); char stack_string[4] = {4, 'a', 'b', 'c'}; char stack_compare[4] = {4, 'a', 'b', 'c'}; output(my_string); append(& my_string, 'y'); output(my_string); reverse_char(& my_string); output(my_string); reverse_char(& my_string); output(my_string); copy(my_string, & copy_string); std::cout << "copy_string: "; output(copy_string); std::cout << character_at(my_string, 1) << std::endl; //compare(my_string, compare_string); // this will either have its own output, or will return bool true false, which will inform a secondary output function...i guess... //compare(stack_string, stack_compare); compare(my_string, stack_compare); delete[] my_string; delete[] copy_string; return 0; }
std::string Func_reverse::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isNull, execplan::CalpontSystemCatalog::ColType&) { string str = stringValue(fp[0], row, isNull); char *end = (char*) str.c_str(); while( *end ) end = reverse_char( end ); reverse( (char*) str.c_str(), end-1 ); return str; }
void before_context(const char *buf, const char *line_head, const char *last_match_line_end_pos, int line_no, match_line_list *match_lines, char eol) { int lim = MAX(op.context, op.before_context); const char *lines[lim + 1]; lines[0] = line_head; int before_count = 0; for (int i = 0; i < lim; i++) { if (lines[i] == buf || last_match_line_end_pos == lines[i]) { break; } const char *p = reverse_char(buf, eol, lines[i] - buf - 1); p = p == NULL ? buf : p + 1; lines[i + 1] = p; before_count++; } for (int i = before_count; i > 0; i--) { int line_len = lines[i - 1] - lines[i] - 1; match_line_node *node = (match_line_node *)hw_malloc(sizeof(match_line_node)); node->line_no = line_no - i; node->context = CONTEXT_BEFORE; node->line = (char *)hw_calloc(line_len + 1, SIZE_OF_CHAR); if (!op.no_omit && line_len > op.omit_threshold) { strncat(node->line, lines[i], op.omit_threshold - DOT_LENGTH); APPEND_DOT(op.color, node->line); } else { strncat(node->line, lines[i], line_len); } enqueue_match_line(match_lines, node); } }
/** * Search the pattern from the file descriptor and add formatted matched lines to the queue if the * pattern was matched in the read buffer. This method processes follow steps: * 1. The file content will be read to a large buffer at once. * 2. Search the pattern from the read buffer. * 3. Scan new line count if need. * * This method returns match count. */ int search(int fd, const char *pattern, int pattern_len, enum file_type t, match_line_list *match_lines, int thread_no) { char eol = '\n'; size_t line_count = 0; size_t read_sum = 0; size_t n = NMAX; ssize_t read_len; int buf_offset = 0; int match_count = 0; bool do_search = false; char *buf = (char *)hw_calloc(n + 1, SIZE_OF_CHAR); char *last_new_line_scan_pos = buf; char *last_line_end; if (!op.use_regex) { prepare_fjs(pattern, pattern_len, t); } while ((read_len = read(fd, buf + buf_offset, NMAX)) > 0) { read_sum += read_len; // Search end position of the last line in the buffer. We search from the first position // and end position of the last line. size_t search_len; if (read_len < NMAX) { last_line_end = buf + read_sum; search_len = read_sum; buf[read_sum] = eol; } else { last_line_end = reverse_char(buf + buf_offset, eol, read_len); if (last_line_end == NULL) { buf = last_new_line_scan_pos = grow_buf_if_shortage(&n, read_sum, buf_offset, buf, buf); buf_offset += read_len; continue; } search_len = last_line_end - buf; } do_search = true; // Search the pattern and construct matching results. The results will be stored to list // `match_lines`. int count = search_buffer( buf, search_len, pattern, pattern_len, t, eol, &line_count, &last_new_line_scan_pos, match_lines, thread_no ); match_count += count; // If hw search the pattern from stdin stream and find the pattern in the buffer, results // are printed immedeately. if (fd == STDIN_FILENO && count > 0) { file_queue_node stream; stream.t = t; stream.match_lines = match_lines; print_result(&stream); // Release memory because matching line was already printed. clear_line_list(match_lines); } // Break loop if file pointer is reached to EOF. But if the file descriptor is stdin, we // should wait for next input. For example, if hw search from the pipe that is created by // `tail -f`, we should continue searching until receive a signal. if (fd != STDIN_FILENO && read_len < NMAX) { break; } if (op.show_line_number) { last_new_line_scan_pos = scan_newline(last_new_line_scan_pos, last_line_end, &line_count, eol); } last_line_end++; ssize_t rest = read_sum - search_len - 1; if (rest >= 0) { char *new_buf = grow_buf_if_shortage(&n, rest, 0, last_line_end, buf); if (new_buf == last_line_end) { new_buf = buf; memmove(new_buf, last_line_end, rest); } buf = last_new_line_scan_pos = new_buf; buf_offset = rest; read_sum = rest; } } tc_free(buf); return match_count; }
/** * Search the pattern from the buffer as a specified encoding `t`. If matching string was found, * results will be added to `match_lines` list. This method do also scanning new lines, and count * up it. */ int search_buffer(const char *buf, size_t search_len, const char *pattern, int pattern_len, enum file_type t, char eol, size_t *line_count, char **last_new_line_scan_pos, match_line_list *match_lines, int thread_no) { match m; const char *p = buf; int after_count = 0; int match_count = 0; // Search the first pattern in the buffer. while (search_by(p, search_len, pattern, pattern_len, t, &m, thread_no)) { // Search head/end of the line, then calculate line length from them. int plen = m.end - m.start; size_t rest_len = search_len - m.start - plen + 1; const char *line_head = reverse_char(p, eol, m.start); char *line_end = memchr(p + m.start + plen, eol, rest_len); line_head = line_head == NULL ? p : line_head + 1; // Collect after context. const char *last_line_end_by_after = p; if (match_count > 0 && (op.after_context > 0 || op.context > 0)) { last_line_end_by_after = after_context(line_head, p, search_len, *line_count, match_lines, eol, &after_count); } // Count lines. if (op.show_line_number) { *last_new_line_scan_pos = scan_newline(*last_new_line_scan_pos, line_end, line_count, eol); } // Collect before context. if (op.before_context > 0 || op.context > 0) { before_context(buf, line_head, last_line_end_by_after, *line_count, match_lines, eol); } // Search next pattern in the current line and format them in order to print. m.start -= line_head - p; m.end = m.start + plen; match_count += format_line(line_head, line_end - line_head, pattern, plen, t, *line_count, &m, match_lines, thread_no); size_t diff = line_end - p + 1; if (search_len < diff) { break; } search_len -= diff; p = line_end + 1; } // Collect last after context. And calculate max line number in this file in order to do // padding line number on printing result. if (match_count > 0 && search_len > 0 && (op.after_context > 0 || op.context > 0)) { after_context(NULL, p, search_len, *line_count, match_lines, eol, &after_count); } match_lines->max_line_no = *line_count + after_count; return match_count; }