Ejemplo n.º 1
0
	/*
	 * Returns true if the specified string is a Palindrome.
	 */
	bool isPalindrome( STR str ) {
		str.erase( std::remove_if( str.begin(), str.end(), ::isspace ), str.end() );
		STR copy = str;
		std::reverse( copy.begin(), copy.end() );
		
		return ( strcmp( str.c_str(), copy.c_str() ) == 0 );
	} 
Ejemplo n.º 2
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>());
}
Ejemplo n.º 3
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>());
	}
}
Ejemplo n.º 4
0
	/*
	 * Returns a scrambled string.
	 */
	STR scramble( STR str ) {
		std::seed_seq sd( str.begin(), str.end() );
		std::default_random_engine gen;

		for ( int i = str.size() - 1; i > 0; --i ) {
			gen.seed( sd );
			std::uniform_int_distribution<int> dist( 0, i );
			std::swap( str[i], str[dist( gen )] );
		}

		return str;
	}
Ejemplo n.º 5
0
static bool ContainsOnlyCharsT(const STR& input, const STR& characters)
{
    for(typename STR::const_iterator iter=input.begin();
        iter!=input.end(); ++iter)
    {
        if(characters.find(*iter) == STR::npos)
        {
            return false;
        }
    }
    return true;
}
Ejemplo n.º 6
0
STR CollapseWhitespaceT(const STR& text,
                        bool trim_sequences_with_line_breaks)
{
    STR result;
    result.resize(text.size());

    // 设置标志位为true假设已经在连续空白中, 这样可以移除开头的全部空白.
    bool in_whitespace = true;
    bool already_trimmed = true;

    int chars_written = 0;
    for(typename STR::const_iterator i=text.begin(); i!=text.end(); ++i)
    {
        if(IsWhitespace(*i))
        {
            if(!in_whitespace)
            {
                // 减少连续空白至一个空白.
                in_whitespace = true;
                result[chars_written++] = L' ';
            }
            if(trim_sequences_with_line_breaks && !already_trimmed &&
                ((*i=='\n') || (*i=='\r')))
            {
                // 包含回车换行的空白序列全部移除.
                already_trimmed = true;
                --chars_written;
            }
        }
        else
        {
            // 非空白字符直接拷贝.
            in_whitespace = false;
            already_trimmed = false;
            result[chars_written++] = *i;
        }
    }

    if(in_whitespace && !already_trimmed)
    {
        // 忽略末尾的全部空白.
        --chars_written;
    }

    result.resize(chars_written);
    return result;
}
Ejemplo n.º 7
0
	STR CollapseWhitespaceT(const STR& text,
		bool trim_sequences_with_line_breaks) {
		STR result;
		result.resize(text.size());

		// Set flags to pretend we're already in a trimmed whitespace sequence, so we
		// will trim any leading whitespace.
		bool in_whitespace = true;
		bool already_trimmed = true;

		int chars_written = 0;
		for (typename STR::const_iterator i(text.begin()); i != text.end(); ++i) {
			if (IsWhitespace(*i)) {
				if (!in_whitespace) {
					// Reduce all whitespace sequences to a single space.
					in_whitespace = true;
					result[chars_written++] = L' ';
				}
				if (trim_sequences_with_line_breaks && !already_trimmed &&
					((*i == '\n') || (*i == '\r'))) {
					// Whitespace sequences containing CR or LF are eliminated entirely.
					already_trimmed = true;
					--chars_written;
				}
			}
			else {
				// Non-whitespace chracters are copied straight across.
				in_whitespace = false;
				already_trimmed = false;
				result[chars_written++] = *i;
			}
		}

		if (in_whitespace && !already_trimmed) {
			// Any trailing whitespace is eliminated.
			--chars_written;
		}

		result.resize(chars_written);
		return result;
	}
Ejemplo n.º 8
0
	/*
	 * Returns the whole specified string, reversed.
	 */
	STR reverse_all( STR str ) {
		std::reverse( str.begin(), str.end() );

		return str;
	}