Example #1
0
bool CSV_Parser::parse(const STR& input_line, CSV_FIELDS& output_fields)
{
    /*
        A private method which handles the parsing logic used by both the overloaded public methods
    */
    STR field;
    int i, j;

    if(input_line.length() == 0)
    {
        return false;
    }

    i = 0;
    do
    {
        if(i < input_line.length() && input_line[i] == CSV_QUOTE)
        {
            j = parse_quoted_fields(input_line, field, ++i);
        }
        else
        {
            j = parse_normal_fields(input_line, field, i);
        }
        output_fields.push_back(field);
        i = j + 1;
    }while(j < input_line.length());

    return true;
}
Example #2
0
int CSV_Parser::parse_quoted_fields(const STR& input_line, STR& field, int& i)
{
    /*
        Quoted fields are the ones which are enclosed within quotes
        For instance - Consider that input_line is - 1997,Ford,E350,"Super, luxurious truck"
        An example for a quoted field would be - Super luxurious truck
        Another instance being - 1997,Ford,E350,"Super, ""luxurious"" truck"
    */
    int j;
    field = "";

    for(j=i; j<input_line.length(); j++)
    {
        if(input_line[j] == '"' && input_line[++j] != '"')
        {
            int k = input_line.find_first_of(CSV_DELIMITER, j);
            if(k > input_line.length())
            {
                k = input_line.length();
            }
            for(k -= j; k-- > 0; )
            {
                field += input_line[j++];
            }
            break;
        }
        else
        {
            field += input_line[j];
        }
    }
    return j;
}
Example #3
0
bool EndsWithT(const STR& str, const STR& search, bool case_sensitive) {
	size_t str_length = str.length();
	size_t search_length = search.length();
	if (search_length > str_length)
		return false;
	if (case_sensitive)
		return str.compare(str_length - search_length, search_length, search) == 0;
	return std::equal(search.begin(), search.end(),
		str.begin() + (str_length - search_length),
		base::CaseInsensitiveCompare<typename STR::value_type>());
}
Example #4
0
	TrimPositions TrimStringT(const STR& input,
		const STR& trim_chars,
		TrimPositions positions,
		STR* output) {
		// Find the edges of leading/trailing whitespace as desired.
		const size_t last_char = input.length() - 1;
		const size_t first_good_char = (positions & TRIM_LEADING) ?
			input.find_first_not_of(trim_chars) : 0;
		const size_t last_good_char = (positions & TRIM_TRAILING) ?
			input.find_last_not_of(trim_chars) : last_char;

		// When the string was all whitespace, report that we stripped off whitespace
		// from whichever position the caller was interested in.  For empty input, we
		// stripped no whitespace, but we still need to clear |output|.
		if (input.empty() ||
			(first_good_char == STR::npos) || (last_good_char == STR::npos)) {
			bool input_was_empty = input.empty();  // in case output == &input
			output->clear();
			return input_was_empty ? TRIM_NONE : positions;
		}

		// Trim the whitespace.
		*output =
			input.substr(first_good_char, last_good_char - first_good_char + 1);

		// Return where we trimmed from.
		return static_cast<TrimPositions>(
			((first_good_char == 0) ? TRIM_NONE : TRIM_LEADING) |
			((last_good_char == last_char) ? TRIM_NONE : TRIM_TRAILING));
	}
Example #5
0
TrimPositions TrimStringT(const STR& input,
                          const typename STR::value_type trim_chars[],
                          TrimPositions positions,
                          STR* output)
{
    // 根据移除选项positions查找两端边界.
    const typename STR::size_type last_char = input.length() - 1;
    const typename STR::size_type first_good_char = (positions&TRIM_LEADING) ?
        input.find_first_not_of(trim_chars) : 0;
    const typename STR::size_type last_good_char = (positions&TRIM_TRAILING) ?
        input.find_last_not_of(trim_chars) : last_char;

    // 当字符串所有字符都是空白, 根据调用传入的positions返回TrimPositions.
    // 对于空输入没有去除任何空白, 但仍需要对output串调用clear.
    if(input.empty() || (first_good_char==STR::npos) || (last_good_char==STR::npos))
    {
        bool input_was_empty = input.empty();
        output->clear();
        return input_was_empty ? TRIM_NONE : positions;
    }

    // 移除空白.
    *output = input.substr(first_good_char, last_good_char-first_good_char+1);

    // 返回两端哪边移除过.
    return static_cast<TrimPositions>(
        ((first_good_char==0)?TRIM_NONE:TRIM_LEADING) |
        ((last_good_char==last_char)?TRIM_NONE:TRIM_TRAILING));
}
Example #6
0
int CSV_Parser::parse_normal_fields(const STR& input_line, STR& field, int& i)
{
    /*
        Normal fields are the ones which contain no escaped or quoted characters
        For instance - Consider that input_line is - 1997,Ford,E350,"Super, luxurious truck"
        An example for a normal field would be - Ford
    */
    int j;
    j = input_line.find_first_of(CSV_DELIMITER, i);
    if(j > input_line.length())
    {
        j = input_line.length();
    }
    field = std :: string(input_line, i, j-i);
    return j;
}
Example #7
0
	static bool DoIsStringASCII(const STR& str) {
		for (size_t i = 0; i < str.length(); i++) {
			typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i];
			if (c > 0x7F)
				return false;
		}
		return true;
	}
Example #8
0
bool StartsWithT(const STR& str, const STR& search, bool case_sensitive) {
	if (case_sensitive) {
		return str.compare(0, search.length(), search) == 0;
	}
	else {
		if (search.size() > str.size())
			return false;
		return std::equal(search.begin(), search.end(), str.begin(),
			base::CaseInsensitiveCompare<typename STR::value_type>());
	}
}
Example #9
0
STR CCrypt::md5(STR value){
    MD5_CTX ctx;
    unsigned char buff[MD5_DIGEST_LENGTH];
    MD5_Init(&ctx);
    MD5_Update(&ctx, value.c_str(), value.length());
    MD5_Final(buff, &ctx);
    char res[33];
    for(int i = 0; i < 16; i++){
        sprintf(res+i*2, "%02x", buff[i]);
    }
    return STR(res);
}
Example #10
0
void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result)
{
    const size_t length = str.length();
    if(!length)
    {
        return;
    }

    bool last_was_ws = false;
    size_t last_non_ws_start = 0;
    for(size_t i=0; i<length; ++i)
    {
        switch(str[i])
        {
            // HTML 5定义的空白: space, tab, LF, line tab, FF, or CR.
        case L' ':
        case L'\t':
        case L'\xA':
        case L'\xB':
        case L'\xC':
        case L'\xD':
            if(!last_was_ws)
            {
                if(i > 0)
                {
                    result->push_back(str.substr(last_non_ws_start,
                        i-last_non_ws_start));
                }
                last_was_ws = true;
            }
            break;

        default: // 不是空白字符.
            if(last_was_ws)
            {
                last_was_ws = false;
                last_non_ws_start = i;
            }
            break;
        }
    }
    if(!last_was_ws)
    {
        result->push_back(str.substr(last_non_ws_start,
            length-last_non_ws_start));
    }
}
Example #11
0
	bool ReplaceCharsT(const STR& input,
		const STR& replace_chars,
		const STR& replace_with,
		STR* output) {
		bool removed = false;
		size_t replace_length = replace_with.length();

		*output = input;

		size_t found = output->find_first_of(replace_chars);
		while (found != STR::npos) {
			removed = true;
			output->replace(found, 1, replace_with);
			found = output->find_first_of(replace_chars, found + replace_length);
		}

		return removed;
	}