syString trim(const syString& str,const syString& chars) { return ltrim(rtrim(str,chars),chars); }
char *trim(char *s) { return rtrim(ltrim(s)); }
/** * In-place left and right trim a string. Strip all the characters specified. * @param s String to trim * @param charList List of character to trim * @return Reference to the (modified) input string */ static inline std::string &trim(std::string &s, const char* charList) { return ltrim(rtrim(s, charList), charList); }
std::string GumboInterface::prettyprint(GumboNode* node, int lvl, const std::string indent_chars) { // special case the document node if (node->type == GUMBO_NODE_DOCUMENT) { std::string results = build_doctype(node); results.append(prettyprint_contents(node,lvl+1,indent_chars)); return results; } std::string tagname = get_tag_name(node); std::string parentname = get_tag_name(node->parent); bool in_head = (parentname == "head"); bool is_structural = in_set(structural_tags, tagname); bool is_inline = in_set(nonbreaking_inline, tagname); bool in_xml_ns = node->v.element.tag_namespace != GUMBO_NAMESPACE_HTML; // build attr string std::string atts = ""; bool no_entity_substitution = in_set(no_entity_sub, tagname); const GumboVector * attribs = &node->v.element.attributes; for (unsigned int i=0; i< attribs->length; ++i) { GumboAttribute* at = static_cast<GumboAttribute*>(attribs->data[i]); atts.append(build_attributes(at, no_entity_substitution)); } bool is_void_tag = in_set(void_tags, tagname); // get tag contents std::string contents = ""; if (!is_void_tag) { if (is_structural && tagname != "html") { contents = prettyprint_contents(node, lvl+1, indent_chars); } else { contents = prettyprint_contents(node, lvl, indent_chars); } } bool keep_whitespace = in_set(preserve_whitespace, tagname); if (!keep_whitespace && !is_inline) { rtrim(contents); } std::string testcontents = contents; ltrim(testcontents); bool single = is_void_tag || (in_xml_ns && testcontents.empty()); char c = indent_chars.at(0); int n = indent_chars.length(); std::string indent_space = std::string((lvl-1)*n,c); // handle self-closed tags with no contents first if (single) { std::string selfclosetag = "<" + tagname + atts + "/>"; if (is_inline) { // always add newline after br tags when they are children of structural tags if ((tagname == "br") && in_set(structural_tags, parentname)) { selfclosetag.append("\n"); if (!in_head && (tagname != "html")) selfclosetag.append("\n"); } return selfclosetag; } if (!in_head && (tagname != "html")) selfclosetag.append("\n"); return indent_space + selfclosetag + "\n"; } // Handle the general case std::string results; std::string starttag = "<" + tagname + atts + ">"; std::string closetag = "</" + tagname + ">"; if (is_structural) { results = indent_space + starttag; if (!contents.empty()) { results.append("\n" + contents + "\n" + indent_space); } results.append(closetag + "\n"); if (!in_head && (tagname != "html")) results.append("\n"); } else if (is_inline) { results = starttag; results.append(contents); results.append(closetag); } else /** all others */ { results = indent_space + starttag; if (!keep_whitespace) { ltrim(contents); } results.append(contents); results.append(closetag + "\n"); if (!in_head && (tagname != "html")) results.append("\n"); } return results; }
// trim from both ends string& trim(string &s) { return ltrim(rtrim(s)); }
static void update_topol(const char *topinout, int p_num, int n_num, const char *p_name, const char *n_name, char *grpname) { #define TEMP_FILENM "temp.top" FILE *fpin, *fpout; char buf[STRLEN], buf2[STRLEN], *temp, **mol_line = NULL; int line, i, nsol, nmol_line, sol_line, nsol_last; gmx_bool bMolecules; printf("\nProcessing topology\n"); fpin = gmx_ffopen(topinout, "r"); fpout = gmx_ffopen(TEMP_FILENM, "w"); line = 0; bMolecules = FALSE; nmol_line = 0; sol_line = -1; nsol_last = -1; while (fgets(buf, STRLEN, fpin)) { line++; strcpy(buf2, buf); if ((temp = strchr(buf2, '\n')) != NULL) { temp[0] = '\0'; } ltrim(buf2); if (buf2[0] == '[') { buf2[0] = ' '; if ((temp = strchr(buf2, '\n')) != NULL) { temp[0] = '\0'; } rtrim(buf2); if (buf2[strlen(buf2)-1] == ']') { buf2[strlen(buf2)-1] = '\0'; ltrim(buf2); rtrim(buf2); bMolecules = (gmx_strcasecmp(buf2, "molecules") == 0); } fprintf(fpout, "%s", buf); } else if (!bMolecules) { fprintf(fpout, "%s", buf); } else { /* Check if this is a line with solvent molecules */ sscanf(buf, "%s", buf2); if (gmx_strcasecmp(buf2, grpname) == 0) { sol_line = nmol_line; sscanf(buf, "%*s %d", &nsol_last); } /* Store this molecules section line */ srenew(mol_line, nmol_line+1); mol_line[nmol_line] = strdup(buf); nmol_line++; } } gmx_ffclose(fpin); if (sol_line == -1) { gmx_ffclose(fpout); gmx_fatal(FARGS, "No line with moleculetype '%s' found the [ molecules ] section of file '%s'", grpname, topinout); } if (nsol_last < p_num+n_num) { gmx_ffclose(fpout); gmx_fatal(FARGS, "The last entry for moleculetype '%s' in the [ molecules ] section of file '%s' has less solvent molecules (%d) than were replaced (%d)", grpname, topinout, nsol_last, p_num+n_num); } /* Print all the molecule entries */ for (i = 0; i < nmol_line; i++) { if (i != sol_line) { fprintf(fpout, "%s", mol_line[i]); } else { printf("Replacing %d solute molecules in topology file (%s) " " by %d %s and %d %s ions.\n", p_num+n_num, topinout, p_num, p_name, n_num, n_name); nsol_last -= p_num + n_num; if (nsol_last > 0) { fprintf(fpout, "%-10s %d\n", grpname, nsol_last); } if (p_num > 0) { fprintf(fpout, "%-15s %d\n", p_name, p_num); } if (n_num > 0) { fprintf(fpout, "%-15s %d\n", n_name, n_num); } } } gmx_ffclose(fpout); /* use gmx_ffopen to generate backup of topinout */ fpout = gmx_ffopen(topinout, "w"); gmx_ffclose(fpout); rename(TEMP_FILENM, topinout); #undef TEMP_FILENM }
std::string GumboInterface::serialize(GumboNode* node, enum UpdateTypes doupdates) { // special case the document node if (node->type == GUMBO_NODE_DOCUMENT) { std::string results = build_doctype(node); results.append(serialize_contents(node, doupdates)); return results; } std::string close = ""; std::string closeTag = ""; std::string atts = ""; std::string tagname = get_tag_name(node); bool need_special_handling = in_set(special_handling, tagname); bool is_void_tag = in_set(void_tags, tagname); bool no_entity_substitution = in_set(no_entity_sub, tagname); bool is_href_src_tag = in_set(href_src_tags, tagname); bool in_xml_ns = node->v.element.tag_namespace != GUMBO_NAMESPACE_HTML; // bool is_inline = in_set(nonbreaking_inline, tagname); // build attr string const GumboVector * attribs = &node->v.element.attributes; for (unsigned int i=0; i< attribs->length; ++i) { GumboAttribute* at = static_cast<GumboAttribute*>(attribs->data[i]); atts.append(build_attributes(at, no_entity_substitution, ((doupdates & SourceUpdates) && is_href_src_tag), (doupdates & StyleUpdates))); } // Make sure that the xmlns attribute exists as an html tag attribute if (tagname == "html") { if (atts.find("xmlns=") == std::string::npos) { atts.append(" xmlns=\"http://www.w3.org/1999/xhtml\""); } } // determine contents std::string contents; if ((tagname == "body") && (doupdates & BodyUpdates)) { contents = m_newbody; } else { // serialize your contents contents = serialize_contents(node, doupdates); } // determine closing tag type std::string testcontents = contents; ltrim(testcontents); if (is_void_tag || (in_xml_ns && testcontents.empty())) { close = "/"; } else { closeTag = "</" + tagname + ">"; } if ((doupdates & StyleUpdates) && (tagname == "style") && (node->parent->type == GUMBO_NODE_ELEMENT) && (node->parent->v.element.tag == GUMBO_TAG_HEAD)) { contents = update_style_urls(contents); } if (need_special_handling) { ltrimnewlines(contents); rtrim(contents); contents.append("\n"); } // build results std::string results; if ((doupdates & LinkUpdates) && (tagname == "link") && (node->parent->type == GUMBO_NODE_ELEMENT) && (node->parent->v.element.tag == GUMBO_TAG_HEAD)) { return ""; } results.append("<"+tagname+atts+close+">"); if (need_special_handling) results.append("\n"); results.append(contents); if ((doupdates & LinkUpdates) && (tagname == "head")) { results.append(m_newcsslinks); } results.append(closeTag); if (need_special_handling) results.append("\n"); return results; }
std::string GumboInterface::prettyprint_contents(GumboNode* node, int lvl, const std::string indent_chars) { std::string contents = ""; std::string tagname = get_tag_name(node); bool no_entity_substitution = in_set(no_entity_sub, tagname); bool keep_whitespace = in_set(preserve_whitespace, tagname); bool is_inline = in_set(nonbreaking_inline, tagname); bool is_structural = in_set(structural_tags, tagname); // bool pp_okay = !is_inline && !keep_whitespace; char c = indent_chars.at(0); int n = indent_chars.length(); GumboVector* children = &node->v.element.children; for (unsigned int i = 0; i < children->length; ++i) { GumboNode* child = static_cast<GumboNode*> (children->data[i]); if (child->type == GUMBO_NODE_TEXT) { std::string val; if (no_entity_substitution) { val = std::string(child->v.text.text); } else { val = substitute_xml_entities_into_text(std::string(child->v.text.text)); } // if child of a structual element is text, indent it properly if (is_structural) { std::string indent_space = std::string((lvl-1)*n,c); contents.append(indent_space); ltrim(val); } else if (!keep_whitespace && !is_structural) { // okay to condense whitespace condense_whitespace(val); } contents.append(val); } else if (child->type == GUMBO_NODE_ELEMENT || child->type == GUMBO_NODE_TEMPLATE) { std::string val = prettyprint(child, lvl, indent_chars); contents.append(val); } else if (child->type == GUMBO_NODE_WHITESPACE) { if (keep_whitespace) { std::string wspace = std::string(child->v.text.text); contents.append(wspace); } else if (is_inline || in_set(other_text_holders, tagname)) { char last_char = 'x'; if (!contents.empty()) { last_char = contents.at(contents.length()-1); } if (std::string(" \t\v\f\r\n").find(last_char) == std::string::npos) { contents.append(std::string(" ")); } } } else if (child->type == GUMBO_NODE_CDATA) { contents.append("<![CDATA[" + std::string(child->v.text.text) + "]]>"); } else if (child->type == GUMBO_NODE_COMMENT) { contents.append("<!--" + std::string(child->v.text.text) + "-->"); } else { fprintf(stderr, "unknown element of type: %d\n", child->type); } } return contents; }
std::string GumboInterface::prettyprint(GumboNode* node, int lvl, const std::string indent_chars) { // special case the document node if (node->type == GUMBO_NODE_DOCUMENT) { std::string results = build_doctype(node); results.append(prettyprint_contents(node,lvl+1,indent_chars)); return results; } std::string close = ""; std::string closeTag = ""; std::string atts = ""; std::string tagname = get_tag_name(node); std::string parentname = get_tag_name(node->parent); bool in_head = (parentname == "head"); // bool need_special_handling = in_set(special_handling, tagname); bool is_empty_tag = in_set(empty_tags, tagname); bool no_entity_substitution = in_set(no_entity_sub, tagname); bool keep_whitespace = in_set(preserve_whitespace, tagname); bool is_inline = in_set(nonbreaking_inline, tagname) && (parentname != "body"); bool is_structural = in_set(structural_tags, tagname); bool pp_okay = !is_inline && !keep_whitespace; char c = indent_chars.at(0); int n = indent_chars.length(); // build attr string const GumboVector * attribs = &node->v.element.attributes; for (unsigned int i=0; i< attribs->length; ++i) { GumboAttribute* at = static_cast<GumboAttribute*>(attribs->data[i]); atts.append(build_attributes(at, no_entity_substitution)); } // determine closing tag type if (is_empty_tag) { close = "/"; } else { closeTag = "</" + tagname + ">"; } std::string indent_space = std::string((lvl-1)*n,c); std::string contents; // prettyprint your contents if (is_structural && tagname != "html") { contents = prettyprint_contents(node, lvl+1, indent_chars); } else { contents = prettyprint_contents(node, lvl, indent_chars); } if (is_structural) { rtrim(contents); if (!contents.empty()) contents.append("\n"); } // remove any leading or trailing whitespace form within paragraphs if (tagname == "p") { ltrim(contents); rtrim(contents); } char last_char = ' '; if (!contents.empty()) { last_char = contents.at(contents.length()-1); } // build results std::string results; if (!is_inline && !in_set(nonbreaking_inline, parentname)) { results.append(indent_space); } results.append("<"+tagname+atts+close+">"); if (pp_okay && is_structural && !contents.empty()) { results.append("\n"); } results.append(contents); if (pp_okay && (last_char != '\n') && !contents.empty() && is_structural) { results.append("\n"); } // handle any indent before structural close tags if (!is_inline && is_structural && !closeTag.empty() && !contents.empty()) { results.append(indent_space); } results.append(closeTag); if ((pp_okay || tagname =="br") && !in_set(nonbreaking_inline, parentname)) { if (!in_head && tagname != "html") { results.append("\n\n"); } else { results.append("\n"); } } return results; }
void NdisReadConfiguration(OUT PNDIS_STATUS status, OUT PNDIS_CONFIGURATION_PARAMETER * param_value, IN NDIS_HANDLE config_handle, IN PNDIS_STRING keyword, IN NDIS_PARAMETER_TYPE param_type) { char *name = keyword->Buffer; char *s, *buf = init_file, *end_buf = init_file + init_file_length; static int count = 0; *status = NDIS_STATUS_FAILURE; *param_value = pNdisParm; if (!count) { print_deb("\n++++++++++++\n%s+++++++++++\n", init_file); count++; } if (!name || !*name || !init_file || !init_file_length) return; memset(pNdisParm, 0, sizeof(NDIS_CONFIGURATION_PARAMETER)); while (buf < end_buf) { buf = ltrim(buf); s = mem_str(buf, name, end_buf); if (!s) break; buf = ltrim(s + strlen(name)); if (*buf == '=') buf++; else { /*print_err("\n...init_config err: delim not found (=): ** %s **\n", buf ); */ buf = s + 1; /*strlen(name); */ continue; } buf = ltrim(buf); if (param_type == NdisParameterString) { char *remark = NULL; s = strchr(buf, '\n'); if (!s) s = buf + strlen(buf); remark = memchr(buf, '#', s - buf); /* skip remarks */ if (remark) { do { /* remove whitespace */ remark--; } while (*remark == ' ' || *remark == '\t'); pNdisParm->ParameterData.StringData.Length = remark - buf + 1; } else pNdisParm->ParameterData.StringData.Length = s - buf; pNdisParm->ParameterData.StringData.Buffer = (TI_UINT8 *) & pNdisParm->StringBuffer[0]; pNdisParm->ParameterData.StringData.MaximumLength = NDIS_MAX_STRING_LEN; if (!pNdisParm->ParameterData.StringData.Length > NDIS_MAX_STRING_LEN) { *status = NDIS_STATUS_BUFFER_TOO_SHORT; return; } memcpy(pNdisParm->ParameterData.StringData.Buffer, buf, pNdisParm->ParameterData.StringData.Length); print_info("NdisReadConfiguration(): %s = (%d)'%s'\n", name, pNdisParm->ParameterData.StringData.Length, pNdisParm->ParameterData.StringData.Buffer); } else if (param_type == NdisParameterInteger) { char *end_p; pNdisParm->ParameterData.IntegerData = simple_strtol(buf, &end_p, 0); if (end_p && *end_p && *end_p != ' ' && *end_p != '\n' && *end_p != '\r' && *end_p != '\t') { print_err ("\n...init_config: invalid int value for <%s> : %s\n", name, buf); return; } /*print_deb(" NdisReadConfiguration(): buf = %p (%.20s)\n", buf, buf ); */ print_info("NdisReadConfiguration(): %s = %d\n", name, (TI_INT32) pNdisParm->ParameterData. IntegerData); } else { print_err ("NdisReadConfiguration(): unknow parameter type %d for %s\n", param_type, name); return; } *status = NDIS_STATUS_SUCCESS; return; } return; }
inline std::string &trim(std::string &s, char x) { return ltrim(rtrim(s, x), x); }
std::string & trim(std::string & str) { return ltrim(rtrim(str)); }
int main(){ ltrim(); rtrim(); return 0; }
String String::trim() { String s(ltrim()); return s.rtrim(); }
const std::string trim (const std::string& s) { return ltrim (rtrim (s)); }
int get_statement(FILE *query_input) { char line[128]; char *pos_begin; int comment_index, statement_index; sql_statement.statement[0]='\0'; sql_statement.comment[0]='\0'; comment_index=0; statement_index=0; while (fgets(line, MAX_LINE_WIDTH, query_input) != NULL) { /* skip the blank lines */ if (line[0] == '\n') continue; /* remove the leading spaces */ ltrim(line); /* if this is a comment line, store it to statement.comment */ if (line[0]=='-' && line[1]=='-') { comment_index += sprintf(sql_statement.comment+comment_index, "%s", line); /* get query number */ if ((pos_begin=strstr(line, "Query (")) != NULL) { pos_begin+=strlen("Query (Q"); sql_statement.query_id = atoi(pos_begin); } } else { /* if this is a 'set row' line, store the row count */ if (strncmp(line, "set rowcount", 12) == 0) { pos_begin=line+13; sql_statement.rowcount=atoi(pos_begin); } /* if it is START_TRAN, then this is the beginning of this block */ if (strcmp(line, START_TRAN) == 0) { return BEGIN_OF_BLOCK; } /* if it is END_TRAN, then this is the end of this block */ if (strcmp(line, END_TRAN) == 0) { return END_OF_BLOCK; } /* otherwise, it is sql statement */ if ( (pos_begin=strchr(line, ';')) != NULL) { #ifdef SAPDB /* if it is the end of the statement, add \n */ *pos_begin='\n'; statement_index += sprintf(sql_statement.statement+statement_index, "%s", line); #endif /* SAPDB */ #ifdef PGSQL /* pgsql requires ';' */ statement_index += sprintf(sql_statement.statement+statement_index, "%s", line); statement_index += sprintf(sql_statement.statement+statement_index, "%c", '\n'); #endif /* PGSQL */ #ifdef MYSQL /* mysql requires ';' */ statement_index += sprintf(sql_statement.statement+statement_index, "%s", line); statement_index += sprintf(sql_statement.statement+statement_index, "%c", '\n'); #endif /* MYSQL */ return END_OF_STMT; } /* get rid of \n */ else if ( (pos_begin=strchr(line, '\n')) != NULL) { *pos_begin=' '; statement_index += sprintf(sql_statement.statement+statement_index, "%s", line); } } } return END_OF_FILE; }
std::wstring trim ( const std::wstring & sourceStr ,const wstring &whitespace) { std::wstring str = sourceStr; return ltrim ( rtrim ( str , whitespace ) , whitespace ); }
char *trim(char *str) { return ltrim(rtrim(str)); }
int main(void) { plan(60); /* lowercase */ char test[100]; ok(lc(NULL) == NULL, "lc(NULL)"); strcpy(test, "Yes"); like(lc(test), "yes", "lc(yes)"); strcpy(test, "YES"); like(lc(test), "yes", "lc(YES)"); strcpy(test, "yeS"); like(lc(test), "yes", "lc(yeS)"); /* trim */ strcpy(test, " text "); like(ltrim(test), "text ", "ltrim()"); strcpy(test, " text "); like(rtrim(test), " text", "rtrim()"); strcpy(test, " text "); like(trim(test), "text", "trim()"); char *test2; test2 = strdup(" text "); like(trim(test2), "text", "trim()"); free(test2); /* parse_yes_or_no */ ok(parse_yes_or_no(NULL, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 1"); ok(parse_yes_or_no(NULL, GM_DISABLED) == GM_DISABLED, "parse_yes_or_no 2"); strcpy(test, ""); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 3"); strcpy(test, ""); ok(parse_yes_or_no(test, GM_DISABLED) == GM_DISABLED, "parse_yes_or_no 4"); strcpy(test, "yes"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 5"); strcpy(test, "true"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 6"); strcpy(test, "Yes"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 7"); strcpy(test, "1"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 8"); strcpy(test, "On"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 9"); strcpy(test, "Off"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 10"); strcpy(test, "false"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 11"); strcpy(test, "no"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 12"); strcpy(test, "0"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 13"); /* trim */ ok(trim(NULL) == NULL, "trim(NULL)"); strcpy(test, " test "); like(trim(test), "^test$", "trim(' test ')"); strcpy(test, "\ntest\n"); like(trim(test), "^test$", "trim('\\ntest\\n')"); /* reading keys */ mod_gm_opt_t *mod_gm_opt; mod_gm_opt = malloc(sizeof(mod_gm_opt_t)); int rc = set_default_options(mod_gm_opt); ok(rc == 0, "setting default options"); mod_gm_opt->keyfile = strdup("t/data/test1.key"); read_keyfile(mod_gm_opt); //printf_hex(mod_gm_opt->crypt_key, 32); test[0]='\x0'; int i = 0; char hex[4]; for(i=0; i<32; i++) { hex[0] = '\x0'; snprintf(hex, 4, "%02x", mod_gm_opt->crypt_key[i]); strncat(test, hex, 4); } like(test, "3131313131313131313131313131313131313131313131313131313131310000", "read keyfile t/data/test1.key"); free(mod_gm_opt->keyfile); mod_gm_opt->keyfile = strdup("t/data/test2.key"); read_keyfile(mod_gm_opt); like(mod_gm_opt->crypt_key, "abcdef", "reading keyfile t/data/test2.key"); free(mod_gm_opt->keyfile); mod_gm_opt->keyfile = strdup("t/data/test3.key"); read_keyfile(mod_gm_opt); //printf_hex(mod_gm_opt->crypt_key, 32); like(mod_gm_opt->crypt_key, "11111111111111111111111111111111", "reading keyfile t/data/test3.key"); ok(strlen(mod_gm_opt->crypt_key) == 32, "key size for t/data/test3.key"); /* encrypt */ char * key = "test1234"; char * encrypted = malloc(GM_BUFFERSIZE); char * text = "test message"; char * base = "a7HqhQEE8TQBde9uknpPYQ=="; mod_gm_crypt_init(key); int len; len = mod_gm_encrypt(&encrypted, text, GM_ENCODE_AND_ENCRYPT); ok(len == 24, "length of encrypted only"); like(encrypted, base, "encrypted string"); /* decrypt */ char * decrypted = malloc(GM_BUFFERSIZE); mod_gm_decrypt(&decrypted, encrypted, GM_ENCODE_AND_ENCRYPT); like(decrypted, text, "decrypted text"); free(decrypted); free(encrypted); /* base 64 */ char * base64 = malloc(GM_BUFFERSIZE); len = mod_gm_encrypt(&base64, text, GM_ENCODE_ONLY); ok(len == 16, "length of encode only"); like(base64, "dGVzdCBtZXNzYWdl", "base64 only string"); /* debase 64 */ char * debase64 = malloc(GM_BUFFERSIZE); mod_gm_decrypt(&debase64, base64, GM_ENCODE_ONLY); like(debase64, text, "debase64 text"); free(debase64); free(base64); /* file_exists */ ok(file_exists("01_utils") == 1, "file_exists('01_utils')"); ok(file_exists("non-exist") == 0, "file_exists('non-exist')"); /* nr2signal */ char * signame1 = nr2signal(9); like(signame1, "SIGKILL", "get SIGKILL for 9"); free(signame1); char * signame2 = nr2signal(15); like(signame2, "SIGTERM", "get SIGTERM for 15"); free(signame2); /* string2timeval */ struct timeval t; string2timeval("100.50", &t); ok(t.tv_sec == 100, "string2timeval 1"); ok(t.tv_usec == 50, "string2timeval 2"); string2timeval("100", &t); ok(t.tv_sec == 100, "string2timeval 3"); ok(t.tv_usec == 0, "string2timeval 4"); string2timeval("", &t); ok(t.tv_sec == 0, "string2timeval 5"); ok(t.tv_usec == 0, "string2timeval 6"); string2timeval(NULL, &t); ok(t.tv_sec == 0, "string2timeval 7"); ok(t.tv_usec == 0, "string2timeval 8"); /* command line parsing */ mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=host:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "host:4730", "server=host:4730"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "server=:4730"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=localhost:4730"); parse_args_line(mod_gm_opt, test, 0); strcpy(test, "server=localhost:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "duplicate server"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=localhost:4730,localhost:4730,:4730,host:4730,"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "duplicate server"); like(mod_gm_opt->server_list[1], "host:4730", "duplicate server"); ok(mod_gm_opt->server_num == 2, "server_number = %d", mod_gm_opt->server_num); /* escape newlines */ char * escaped = gm_escape_newlines(" test\n", GM_DISABLED); is(escaped, " test\\n", "untrimmed escape string"); free(escaped); escaped = gm_escape_newlines(" test\n", GM_ENABLED); is(escaped, "test", "trimmed escape string"); free(escaped); /* md5 sum */ char * sum = NULL; strcpy(test, ""); sum = md5sum(test); like(sum, "d41d8cd98f00b204e9800998ecf8427e", "md5sum()"); free(sum); strcpy(test, "The quick brown fox jumps over the lazy dog."); sum = md5sum(test); like(sum, "e4d909c290d0fb1ca068ffaddf22cbd0", "md5sum()"); free(sum); mod_gm_free_opt(mod_gm_opt); return exit_status(); }
void aliasexpand(char *cmd, int maxlen) { char *cp; unsigned m; unsigned len; short expanded; TAlias *ptr; assert(cmd); if (++useFlag == 0) /* (int) overflow */ { /* The useFlag specifies, if the particular ALIAS is used by the _current_ expand. To avoid to clear the flag each time when to expand a string a different flag value is used each time. */ if((ptr = first) != NULL) /* reset all values to be sure we hit no old one */ do ptr->used = 0; while((ptr = ptr->next) != NULL); useFlag = 1; } cp = ltrim(cmd); /* skip leading whitespaces */ /* Check if the user disabled alias expansion */ if (*cp == '*') { cp = ltrim(cp + 1); memmove(cmd, cp, strlen(cp) + 1); return; } /* this allows to case-sensitively compare strings, which is much faster */ partstrlower(cp); /* to simplify the loop below */ memmove(cmd, cp, strlen(cp) + 1); /* substitution loop */ /* Empty alias list --> no loop */ if(first) do { expanded = 0; ptr = first; do { assert(ptr); assert(ptr->name); assert(ptr->subst); len = strlen(ptr->name); if ((isspace(cmd[len]) || cmd[len] == '\0') /* end of word */ && !strncmp(cmd, ptr->name, len) /* line begins with alias */ && ptr->used != useFlag) /* this alias unused */ { m = strlen(ptr->subst); if (strlen(cmd) - len + m > maxlen) { error_command_too_long(); cmd[0] = '\0'; /* the parser won't cause any problems with an empty line */ return; } else { /* adjust the remaining part within the command line */ memmove(&cmd[m], &cmd[len], strlen(&cmd[len]) + 1); /* prepend the alias substitution */ memcpy(&cmd[0], &ptr->subst[0], m); ptr->used = useFlag; expanded = 1; } } } while((ptr = ptr->next) != NULL); } while(expanded); }
std::string GumboInterface::prettyprint_contents(GumboNode* node, int lvl, const std::string indent_chars) { std::string contents = ""; std::string tagname = get_tag_name(node); bool no_entity_substitution = in_set(no_entity_sub, tagname); bool keep_whitespace = in_set(preserve_whitespace, tagname); bool is_inline = in_set(nonbreaking_inline, tagname); bool is_structural = in_set(structural_tags, tagname); char c = indent_chars.at(0); int n = indent_chars.length(); std::string indent_space = std::string((lvl-1)*n,c); char last_char = 'x'; bool contains_block_tags = false; GumboVector* children = &node->v.element.children; if (is_structural || (tagname == "#document")) last_char = '\n'; bool in_head_without_title = (tagname == "head"); for (unsigned int i = 0; i < children->length; ++i) { GumboNode* child = static_cast<GumboNode*> (children->data[i]); if (child->type == GUMBO_NODE_TEXT) { std::string val; if (no_entity_substitution) { val = std::string(child->v.text.text); } else { val = substitute_xml_entities_into_text(std::string(child->v.text.text)); } // if child of a structual element is text and follows a newline, indent it properly if (is_structural && last_char == '\n') { contents.append(indent_space); ltrim(val); } if (!keep_whitespace && !is_structural) { // okay to condense whitespace condense_whitespace(val); } contents.append(val); } else if (child->type == GUMBO_NODE_ELEMENT || child->type == GUMBO_NODE_TEMPLATE) { std::string val = prettyprint(child, lvl, indent_chars); std::string childname = get_tag_name(child); if (in_head_without_title && (childname == "title")) in_head_without_title = false; if (!in_set(nonbreaking_inline, childname)) { contains_block_tags = true; if (last_char != '\n') { contents.append("\n"); if (tagname != "head" && tagname != "html") contents.append("\n"); last_char='\n'; } } // if child of a structual element is inline and follows a newline, indent it properly if (is_structural && in_set(nonbreaking_inline, childname) && (last_char == '\n')) { contents.append(indent_space); ltrim(val); } contents.append(val); } else if (child->type == GUMBO_NODE_WHITESPACE) { if (keep_whitespace) { std::string wspace = std::string(child->v.text.text); contents.append(wspace); } else if (is_inline || in_set(other_text_holders, tagname)) { if (std::string(" \t\v\f\r\n").find(last_char) == std::string::npos) { contents.append(std::string(" ")); } } } else if (child->type == GUMBO_NODE_CDATA) { contents.append("<![CDATA[" + std::string(child->v.text.text) + "]]>"); } else if (child->type == GUMBO_NODE_COMMENT) { contents.append("<!--" + std::string(child->v.text.text) + "-->"); } else { fprintf(stderr, "unknown element of type: %d\n", child->type); } // update last character of current contents if (!contents.empty()) { last_char = contents.at(contents.length()-1); } } // inject epmpty title into head if one is missing if (in_head_without_title) { if (last_char != '\n') contents.append("\n"); contents.append(indent_space + "<title></title>\n"); last_char = '\n'; } // treat inline tags containing block tags like a block tag if (is_inline && contains_block_tags) { if (last_char != '\n') contents.append("\n\n"); contents.append(indent_space); } return contents; }
std::string ossfs::util::trim(const std::string &s) { return ltrim(rtrim(s)); }
// trim from both ends static inline std::string &trim(std::string &s) { return ltrim(rtrim(s)); }
void StringUtil::atrim(std::wstring& str) { ltrim(str); rtrim(str); }
static std::string& trim(std::string &s) { return rtrim(ltrim(s)); }
// trim from left & right inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v") { return ltrim(rtrim(s, t), t); }
std::string trim(const std::string& a_Input, char l_ToTrim){ return ltrim(rtrim(a_Input, l_ToTrim), l_ToTrim); }
inline std::string ltrim_copy(std::string s, const char* t = " \t\n\r\f\v") { return ltrim(s, t); }
std::string ProgrammableOpAttributes::trim(const std::string &s) { return ltrim(rtrim(s)); }
std::string& Utils::trim(std::string & text) { return ltrim(rtrim(text)); }