inline std::string trim (const std::string & s, const std::string & t) { std::string d (s); return trim_left (trim_right (d, t), t) ; } // end of trim
void trim(std::string& source, const std::string& t) { trim_left(source, t); trim_right(source, t); }
char *trim(char *pStr) { trim_right(pStr); trim_left(pStr); return pStr; }
/*! \brief unwrap constuction as '3*' to '* * *' \param s pointer to string \param flag -- if 0 - unwrap whole string, >0 unwrap after FLAG words \return if success 0\n * if bad input pointer -2 */ int FRead::unwrap (char *s, const int flag) const { char *ptr = 0; char *ptr_new = 0; char *ns_ptr = 0; char ns[CHAR_BUF_LEN]; t_long n, i; t_long words_number = 0; if (!s) { return -2; } ns_ptr = ns; ptr = s; if (trim_left (&ptr)) return -4; for (; *ptr != '\0' && words_number < flag;) { if (*ptr == ' ' || *ptr == '\t') { ++words_number; *ns_ptr = ' '; ++ns_ptr; if (trim_left (&ptr)) return -4; } else { *ns_ptr = *ptr; ++ptr; ++ns_ptr; } } for (; *ptr != '\0';) //main loop { n = strtol (ptr, &ptr_new, 10); // try to get int if (ptr != ptr_new) // if something read { if (*ptr_new == '*') // check for '*' { *ns_ptr = ' '; ++ns_ptr; for (i = 0; i < n; ++i) // copy '*' to new array { *ns_ptr = '*'; ++ns_ptr; *ns_ptr = ' '; ++ns_ptr; } ++ptr_new; ptr = ptr_new; } else // copy all from ptr to ptr_new { for (; ptr != ptr_new; ++ptr, ++ns_ptr) *ns_ptr = *ptr; } } else { *ns_ptr = *ptr; ++ptr; ++ns_ptr; } } *ns_ptr = ' '; ++ns_ptr; *ns_ptr = '/'; ++ns_ptr; *ns_ptr = '\0'; strcpy (s, ns); return 0; }
inline std::string trim (const string & s, const string & t = SPACES) { string d (s); return trim_left (trim_right (d, t), t) ; }
TalkActionResult_t TalkActions::onPlayerSpeak(Player* player, SpeakClasses type, const std::string& words) { if (type != SPEAK_SAY) { return TALKACTION_CONTINUE; } std::string str_words_quote; std::string str_param_quote; std::string str_words_first_word; std::string str_param_first_word; // With quotation filtering size_t loc = words.find('"', 0); if (loc != std::string::npos && loc >= 0) { str_words_quote = std::string(words, 0, loc); str_param_quote = std::string(words, (loc + 1), words.size() - loc - 1); } else { str_words_quote = words; str_param_quote = std::string(""); } trim_left(str_words_quote, " "); trim_right(str_param_quote, " "); // With whitespace filtering loc = words.find(' ', 0); if (loc != std::string::npos && loc >= 0) { str_words_first_word = std::string(words, 0, loc); str_param_first_word = std::string(words, (loc + 1), words.size() - loc - 1); } else { str_words_first_word = words; str_param_first_word = std::string(""); } TalkActionList::iterator it; for (it = wordsMap.begin(); it != wordsMap.end(); ++it) { std::string cmdstring; std::string paramstring; if (it->second->getFilterType() == TALKACTION_MATCH_QUOTATION) { cmdstring = str_words_quote; paramstring = str_param_quote; } else if (it->second->getFilterType() == TALKACTION_MATCH_FIRST_WORD) { cmdstring = str_words_first_word; paramstring = str_param_first_word; } else { continue; } if (cmdstring == it->first || (!it->second->isCaseSensitive() && boost::algorithm::iequals(it->first, cmdstring))) { bool ret = true; if (player->getAccessLevel() < it->second->getAccessLevel()) { if (player->getAccessLevel() > 0) { player->sendTextMessage(MSG_STATUS_SMALL, "You can not execute this command."); ret = false; } } else { TalkAction* talkAction = it->second; if (talkAction->isScripted()) { ret = talkAction->executeSay(player, cmdstring, paramstring); } else { TalkActionFunction* func = talkAction->getFunction(); if (func) { func(player, cmdstring, paramstring); ret = false; } } } if (ret) { return TALKACTION_CONTINUE; } else { return TALKACTION_BREAK; } } } return TALKACTION_CONTINUE; }
t_long FRead::convert_u (t_int *array, const t_long len_array, t_long pos, char *buf, const char *key) { char *sbuf; t_long c, i, counter; char *start_ptr, *end_ptr = 0; t_int t; // check section if (array == 0) // check array pointer return -1; if (buf == 0) // check buf pointer return -2; if (pos >= len_array) // check for input parameter return 0; sbuf = new char[strlen (buf) + 1]; // allocate new array if (sbuf == 0) // check allocation error { fprintf (stderr, "not enough memory!\n"); return -3; } start_ptr = buf; // set start pointer to begin of buf counter = 0; // set up counter for (;;) { if (pos >= len_array) { if (*end_ptr != '\0') { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); } delete[]sbuf; return counter; } if (trim_left (&start_ptr)) return -50; if (*start_ptr == '-') // negative values found { delete[]sbuf; return -4; } t = (t_int)strtol (start_ptr, &end_ptr, 10); // try to read int from buf if (trim_left (&end_ptr)) return -50; if (start_ptr == end_ptr) // if have not read return error -4 { delete[]sbuf; return -4; } else if (*end_ptr == '*') // if next character is '*' { ++end_ptr; if (trim_left (&end_ptr)) return -50; start_ptr = end_ptr; if (*start_ptr == '-') // negative values { delete[]sbuf; return -4; } if (pos < len_array) { array[pos] = (t_int)strtol (start_ptr, &end_ptr, 10); // try to read int from buf } else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } c = pos; ++pos; ++counter; if (start_ptr == end_ptr) // if have not read return error -4 { delete[]sbuf; return -4; } else { for (i = 0; i < t - 1; ++i) { if (pos < len_array) array[pos] = array[c]; else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } ++pos; ++counter; } } } else { if (pos < len_array) array[pos] = t; else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } ++pos; ++counter; } start_ptr = end_ptr; if (*start_ptr == '\0') { delete[]sbuf; return counter; } } // delete[] sbuf; // return 0; }
std::string trim(const std::string& s) { return trim_left(trim_right(s)); }
inline std::string trim(const std::string& source, const std::string& t = " ") { std::string str = source; return trim_left( trim_right( str , t) , t ); }
self_type& trim_left() { return trim_left( self_type() ); }
std::string &easyUtils::trim (std::string &text) { return trim_left (trim_right (text)); }
/*-----------------------------------------------------------------*/ unsigned long simGetValue (unsigned int addr,char mem, unsigned int size) { unsigned int b[4] = {0,0,0,0}; /* can be a max of four bytes long */ char cachenr; char buffer[40]; char *resp; if ( size <= 0 ) return 0; cachenr = getMemString(buffer, 0, &addr, mem, size); resp = NULL; if ( cachenr < NMEM_CACHE ) { resp = getMemCache(addr,cachenr,size); } if ( !resp ) { /* create the simulator command */ sendSim(buffer); waitForSim(100,NULL); resp = simResponse(); /* got the response we need to parse it the response is of the form [address] [v] [v] [v] ... special case in case of bit variables which case it becomes [address] [assembler bit address] [v] */ /* first skip thru white space */ resp = trim_left(resp); if (strncmp(resp, "0x",2) == 0) resp += 2; /* skip thru the address part */ while (isxdigit(*resp)) resp++; } /* make the branch for bit variables */ if ( cachenr == BIT_CACHE) { /* skip until newline */ while (*resp && *resp != '\n' ) resp++ ; if ( *--resp != '0' ) b[0] = 1; } else { unsigned int i; for (i = 0 ; i < size ; i++ ) { /* skip white space */ resp = trim_left(resp); b[i] = strtol(resp,&resp,16); } } return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ; }
void SipperMediaPortable::trim(std::string &s, const std::string &t) { trim_right(s, t); trim_left(s, t); } // end of trim
char *trim(char *str) { trim_right(str); trim_left(str); return str; }
self_type& trim_left() { trim_left( self_type() ) ; }
/*! \brief Recursive function\n * For keyword KEY read array from * file stream this->fp to buffer ARRAY.\n * string format: 2*15.8 3*{12 2*5.6 2*{1.1 1.2}} is equals\n * 15.8 15.8 12 5.6 5.6 1.1 1.2 1.1 1.2 12 5.6 5.6 1.1 1.2 1.1 1.2 12 5.6 5.6 1.1 1.2 1.1 1.2 \param key Name of calling keyword \param array string buffer \param len_array lenght of buffer \param pos number of doubles have been in array \param buf \return if success number of read doubles\n * if bad pointer 'array' -1\n * if bad pointer 'buf' -2\n * if cann't allocate memory -3\n * if string format error -4 */ long FRead::convert_d (double *array, const long len_array, long pos, char *buf, const char *key) { char *sbuf; long c, i, counter; char *start_ptr, *end_ptr = 0; double t; // check section if (array == 0) // check array pointer return -1; if (buf == 0) // check buf pointer return -2; if (pos >= len_array) // check for input parameter return 0; sbuf = new char[strlen (buf) + 1]; // allocate new array if (sbuf == 0) // check allocation error { fprintf (stderr, "not enough memory!\n"); return -3; } start_ptr = buf; // set start pointer to begin of buf counter = 0; // set up counter // main loop for (;;) { // check for garbage if (pos >= len_array) { if (*end_ptr != '\0') { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); } delete[]sbuf; return counter; } if (trim_left (&start_ptr)) return -50; t = strtod (start_ptr, &end_ptr); // try to read double from buf if (trim_left (&end_ptr)) return -50; if (*start_ptr == '\0') { delete[]sbuf; return counter; } if (start_ptr == end_ptr) // if have not read return error -4 { delete[]sbuf; return -4; } else if (*end_ptr == '*') // if next character is '*' { ++end_ptr; if (trim_left (&end_ptr)) return -50; start_ptr = end_ptr; if (pos < len_array) array[pos] = strtod (start_ptr, &end_ptr); // try to read double from buf else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } c = pos; ++pos; ++counter; if (start_ptr == end_ptr) // if have not read return error -4 { delete[]sbuf; return -4; } else { for (i = 0; i < (long) floor (t - 1 + 0.5); ++i) { if (pos < len_array) array[pos] = array[c]; else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } ++pos; ++counter; } } } else { if (pos < len_array) array[pos] = t; else { fprintf (stderr, "Error: in %s: trailing garbage %s is ignored for keyword %s\n", get_prefix (), end_ptr, key); delete[]sbuf; return counter; } ++pos; ++counter; } start_ptr = end_ptr; if (*start_ptr == '\0') { delete[]sbuf; return counter; } } // delete[] sbuf; // return 0; }
#include <stdio.h> #include <string.h> #include "trim.h" #include "describe/describe.h" describe("trim_left", { it("should remove leading whitespace", { char str[] = "\t\n hello"; assert_str_equal("hello", trim_left(str)); assert_str_equal("hello", str); }); it("should not remove trailing whitespace", { char str[] = "hello \t\n"; assert_str_equal("hello \t\n", trim_left(str)); assert_str_equal("hello \t\n", str); }); });
Container trim(const T& x, const Container& xs) { return trim_right(x, trim_left(x, xs)); }