static int run_tests(ACEXML_String test_strings[NUM_TEST_STRS], int iterations) { // Test 1 - Escape the strings using a new temporary string each iteration. ACE_Time_Value start = ACE_OS::gettimeofday(); int i = 0; for (i = 0; i < iterations; ++i) { ACEXML_String tmp = ACEXML_escape_string(test_strings[i % NUM_TEST_STRS]); if (! is_escaped(tmp)) { ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n")); return 1; } } ACE_DEBUG((LM_DEBUG, "Test1 took %dms\n", (ACE_OS::gettimeofday() - start).msec())); // Test 2 - Escape the strings using a shared temporary string. This shouldn't // be any faster than Test 1 as long as the compiler has return value optimization. ACEXML_String tmp; start = ACE_OS::gettimeofday(); for (i = 0; i < iterations; ++i) { tmp = ACEXML_escape_string(test_strings[i % NUM_TEST_STRS]); if (! is_escaped(tmp)) { ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n")); return 1; } } ACE_DEBUG((LM_DEBUG, "Test2 took %dms\n", (ACE_OS::gettimeofday() - start).msec())); // Test 3 - Escape the strings using a shared temporary string. This time, we use // the alternate form of ACEXML_escape_string() so that our temporary buffer is reused. tmp.clear(1); start = ACE_OS::gettimeofday(); for (i = 0; i < iterations; ++i) { ACEXML_escape_string(test_strings[i % NUM_TEST_STRS], tmp); if (! is_escaped(tmp)) { ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n")); return 1; } } ACE_DEBUG((LM_DEBUG, "Test3 took %dms\n", (ACE_OS::gettimeofday() - start).msec())); // Test 4 - Same as Test 3, except that the tmp buffer shouldn't have to resize. start = ACE_OS::gettimeofday(); for (i = 0; i < iterations; ++i) { ACEXML_escape_string(test_strings[i % NUM_TEST_STRS], tmp); if (! is_escaped(tmp)) { ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n")); return 1; } } ACE_DEBUG((LM_DEBUG, "Test4 took %dms\n", (ACE_OS::gettimeofday() - start).msec())); return 0; }
/** ** @brief Prints with excaped characters. ** @param argv Input arguments values. ** @param builtin_fd IOs streams. ** @return Returns 0 in case of success (-1) else. */ static inline int print_escaped(char *argv, s_builtin_fd *builtin_fd) { int i; for (i = 0; argv[i] != '\0'; i++) { if (argv[i] == '\\') { if (argv[i] != '\0' ) { if (is_escaped(argv[i + 1])) { if (-1 == if_print_escap(argv[i + 1], builtin_fd)) { fflush(builtin_fd->fout); return (-1); } } else fprintf(builtin_fd->fout, "%c", argv[i + 1]); i++; } } else fprintf(builtin_fd->fout, "%c", argv[i]); } fflush(builtin_fd->fout); return (0); }
bcf_hrec_t *bcf_hdr_parse_line(const bcf_hdr_t *h, char *line, int *len) { char *p = line; if (p[0] != '#' || p[1] != '#') { *len = 0; return NULL; } p += 2; char *q = p; while ( *q && *q!='=' ) q++; int n = q-p; if ( *q!='=' || !n ) { *len = q-line+1; return NULL; } // wrong format bcf_hrec_t *hrec = (bcf_hrec_t*) calloc(1,sizeof(bcf_hrec_t)); hrec->key = (char*) malloc(sizeof(char)*(n+1)); memcpy(hrec->key,p,n); hrec->key[n] = 0; p = ++q; if ( *p!='<' ) // generic field, e.g. ##samtoolsVersion=0.1.18-r579 { while ( *q && *q!='\n' ) q++; hrec->value = (char*) malloc((q-p+1)*sizeof(char)); memcpy(hrec->value, p, q-p); hrec->value[q-p] = 0; *len = q-line+1; return hrec; } // structured line, e.g. ##INFO=<ID=PV1,Number=1,Type=Float,Description="P-value for baseQ bias"> while ( *q && *q!='\n' ) { p = ++q; while ( *q && *q!='=' ) q++; n = q-p; if ( *q!='=' || !n ) { *len = q-line+1; return NULL; } // wrong format bcf_hrec_add_key(hrec, p, q-p); p = ++q; int quoted = *p=='"' ? 1 : 0; if ( quoted ) p++, q++; while (1) { if ( !*q ) break; if ( quoted ) { if ( *q=='"' && !is_escaped(p,q) ) break; } else { if ( *q==',' || *q=='>' ) break; } q++; } bcf_hrec_set_val(hrec, hrec->nkeys-1, p, q-p, quoted); if ( quoted ) q++; if ( *q=='>' ) { q++; break; } } *len = q-line+1; return hrec; }
/************************************************************************ * Function : parse_uric * * Parameters : * char *in ; string of characters * int max ; maximum limit * token *out ; token object where the string of characters is * copied * * Description : Parses a string of uric characters starting at in[0] * as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining * URIs) * * Return : int ; * * Note : ************************************************************************/ int parse_uric( char *in, int max, token * out ) { int i = 0; while( ( i < max ) && ( ( is_unreserved( in[i] ) ) || ( is_reserved( in[i] ) ) || ( ( i + 2 < max ) && ( is_escaped( &in[i] ) ) ) ) ) { i++; } out->size = i; out->buff = in; return i; }
// Replace values in a string according to a trie static char* trie_replace(char* input, trie_node_t* trie, char no_escape) { for(int i = 0; i < strlen(input); i++) { if(trie_match(input[i], trie) != trie && (no_escape || !is_escaped('\\', input + i, input))) { char* replace = NULL; int pat_len = -1; trie_node_t* node = trie; for(int j = i; j < strlen(input); j++) { trie_node_t* newnode = trie_match(input[j], node); if(node != newnode) { node = newnode; if(newnode->replace) { replace = newnode->replace; pat_len = j - i; break; } } else { break; } } if(pat_len >= 0 && replace) { int str_len = strlen(input); int rep_len = strlen(replace); if((rep_len - pat_len) > 0) { size_t new_len = 2 * (str_len + rep_len - pat_len); input = realloc(input, sizeof(char) * new_len); } memmove(input + i + rep_len, input + i + pat_len + 1, str_len - i); memcpy(input + i, replace, rep_len); i += rep_len - 1; } } } size_t size = strlen(input); input = xrealloc(input, strlen(input)); return input; }
/*! * \brief Parses a string of uric characters starting at in[0] as defined in * http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs). * * \return */ static size_t parse_uric( /*! [in] String of characters. */ const char *in, /*! [in] Maximum limit. */ size_t max, /*! [out] Token object where the string of characters is copied. */ token *out) { size_t i = (size_t)0; while (i < max && (is_unreserved(in[i]) || is_reserved(in[i]) || ((i + (size_t)2 < max) && is_escaped(&in[i])))) { i++; } out->size = i; out->buff = in; return i; }
std::string deep_to_string_t::operator()(const char* cstr) const { if (! cstr || *cstr == '\0') return "\"\""; if (is_escaped(cstr)) return cstr; std::string result = "\""; char c; while ((c = *cstr++) != '\0') { switch (c) { case '\\': result += "\\\\"; break; case '"': result += "\\\""; break; default: result += c; } } result += "\""; return result; }
char *openclose_matcher_find_matching_base( t_openclose_matcher *matcher, char *s, t_lst *stack) { while (*s) { if (is_escaped(&s)) continue ; if (*s == '\'' && twl_lst_len(stack) && twl_strequ(openclose_mgr_last(stack)->open, "\"")) { s++; continue; } resolve(matcher, stack, &s); if (twl_lst_len(stack) == 0) { return (s); } } return (NULL); }
int is_closed(char *str, char *syntax) { int i; int inside; char begin; inside = 0; begin = 0; i = 0; if (str == NULL) return (1); while (str[i]) { inside += update_inside(&begin, str, &i, syntax); i += 1; } if (i > 0 && str[i - 1] == '\\' && !is_escaped(str, &str[i - 1])) { str[i - 1] = 0; return (0); } return (inside ? 0 : 1); }
void preprocess(char*& str) { const char* read = str; // Reads the string char* write = str; // Writes into the string size_t line = 1; // Nested comments level handle stack<comment_t> level; // Whether a string is a raw string bool raw = false; // Cross the string while (*read != '\0') { if (*read == '"') { // Found a string if (level.empty()) { // Overall priority raw = is_raw(read); while (*read) { *write++ = *read++; if (*read == '"' && (raw || !is_escaped(read))) { *write++ = *read++; break; } } } else if (level.top() == comment_t::BLOCK) { // Overall priority raw = is_raw(read); while (*read) { ++read; if (*read == '"' && (raw || !is_escaped(read))) { ++read; break; } } } } else if (*read == '#') { if (level.empty()) { // Skip all until the end of the line while (*++read && *read != '\n'); } else { if (*++read == '$') { // Level down the comments level level.pop(); ++read; } else if (level.top() == comment_t::BLOCK) { // Skip all until the end of the line while (*++read && *read != '\n'); *write++ = '\n'; } } } else if (*read == '$') { if (*++read == '#') { // Simple block comment level.push(comment_t::BLOCK); ++read; } else if (*read == '$' && *++read == '#') { // Documentation block level.push(comment_t::DOC); ++read; } else if (level.empty()) { error(error_t::STRAY_CHAR, line, '$'); } } if (level.empty() || *read == '\n') { if (*read == '\n') { ++line; } // Copy the characters in the string // Also copy the new lines to keep track // of the lines where errors may appear *write++ = *read++; } else { ++read; } } *write++ = '\0'; // nul-terminate the string // Shrink the string to reduce memory usage char* new_str = new char[write-str+1]; strcpy(new_str, str); delete[] str; str = new_str; }
void emit_id(const char *id) { if (is_escaped(id)) fprintf(vlog_out, "\\%s ", id); else fprintf(vlog_out, "%s", id); }