Example #1
0
	/*************************************************************************
	Return the index of the first character of the word at 'idx'.	
	*************************************************************************/
	String32::size_type TextUtils::getWordStartIdx(const String32& str, String32::size_type idx)
	{
		String32 temp = str.substr(0, idx);

		trimTrailingChars(temp, DefaultWhitespace);

		if (temp.length() <= 1) {
			return 0;
		}

		// identify the type of character at 'pos'
		if (String32::npos != DefaultAlphanumerical.find(temp[temp.length() - 1]))
		{
			idx = temp.find_last_not_of(DefaultAlphanumerical);
		}
		// since whitespace was stripped, character must be a symbol
		else
		{
			idx = temp.find_last_of(DefaultAlphanumerical + DefaultWhitespace);
		}

		// make sure we do not go past end of string (+1)
		if (idx == String32::npos)
		{
			return 0;
		}
		else
		{
			return idx + 1;
		}

	}
Example #2
0
	/*************************************************************************
	Return the index of the first character of the word after the word
	at 'idx'.	
	*************************************************************************/
	String32::size_type TextUtils::getNextWordStartIdx(const String32& str, String32::size_type idx)
	{
		String32::size_type str_len = str.length();

		// do some checks for simple cases
		if ((idx >= str_len) || (str_len == 0))
		{
			return str_len;
		}

		// is character at 'idx' alphanumeric
		if (String32::npos != DefaultAlphanumerical.find(str[idx]))
		{
			// find position of next character that is not alphanumeric
			idx = str.find_first_not_of(DefaultAlphanumerical, idx);
		}
		// is character also not whitespace (therefore a symbol)
		else if (String32::npos == DefaultWhitespace.find(str[idx]))
		{
			// find index of next character that is either alphanumeric or whitespace
			idx = str.find_first_of(DefaultAlphanumerical + DefaultWhitespace, idx);
		}

		// check result at this stage.
		if (String32::npos == idx)
		{
			idx = str_len;
		}
		else
		{
			// if character at 'idx' is whitespace
			if (String32::npos != DefaultWhitespace.find(str[idx]))
			{
				// find next character that is not whitespace
				idx = str.find_first_not_of(DefaultWhitespace, idx);
			}

			if (String32::npos == idx)
			{
				idx = str_len;
			}

		}

		return idx;
	}