⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string

📁 赫赫大名的 OGRE 游戏引擎
💻
📖 第 1 页 / 共 2 页
字号:
					if (!_buffer || !str._buffer) return -1;
					if (static_cast<unsigned int>(start) > length()) start = length();
					// We'll back up by the length of the string since we can't find
					// a string in reverse order wihtout at least str's length at the end
					// of it...
					start -= str.length();

					while(start > 0)
					{
						if (!fstl::strnicmp(&_buffer[start], str._buffer, str.length())) return start;
						start--;
					}

					return -1;
				}

inline		int		ncCompare(const basic_string & str) const
				{
					if (!_buffer || !str._buffer) return _buffer == str._buffer ? 0:-1;
					return fstl::stricmp(_buffer, str._buffer);
				}

inline		int		ncCompare(const basic_string & str, unsigned int len) const
				{
					if (!_buffer || !str._buffer) return _buffer == str._buffer ? 0:-1;
					if (len > length()) len = length();
					return fstl::strnicmp(_buffer, str._buffer, len);
				}

inline		int		findFirstOf(const T *set, int start = 0) const
				{
					if (!_buffer || !set) return -1;
					if (start < 0) start = 0;
					else if (static_cast<unsigned int>(start) > length()) return -1;

					int	idx = static_cast<int>(fstl::strcspn(&_buffer[start], set));

					if (idx == length()) return -1;
					return start + idx;
				}

inline		int		findFirstNotOf(const T *set, int start = 0) const
				{
					if (!_buffer || !set) return -1;
					if (start < 0) start = 0;
					else if (static_cast<unsigned int>(start) > length()) return -1;

					int	idx = static_cast<int>(fstl::strspn(&_buffer[start], set));

					if (idx == length()) return -1;
					return start + idx;
				}

inline		int		findLastOf(const T *set, int start = 0x7fffffff) const
				{
					if (!_buffer || !set) return -1;
					if (start < 0) return -1;
					else if (static_cast<unsigned int>(start) >= length()) start = length() - 1;

					size_t	setLength = fstl::strlen(set);

					const	T *	ptr = &_buffer[start];
					while(ptr >= _buffer)
					{
						for (size_t i = 0; i < setLength; ++i)
						{
							if (*ptr == set[i])
							{
								return static_cast<int>(ptr - _buffer);
							}
						}

						--ptr;
					}

					// None mached, retrun the beginning of the string

					return 0;
				}

inline		int		findLastNotOf(const T *set, int start = 0x7fffffff) const
				{
					if (!_buffer || !set) return -1;
					if (start < 0) return -1;
					else if (static_cast<unsigned int>(start) >= length()) start = length() - 1;

					size_t	setLength = fstl::strlen(set);

					const	T *	ptr = &_buffer[start];
					while(ptr >= _buffer)
					{
						bool	found = false;
						for (size_t i = 0; i < setLength; ++i)
						{
							if (*ptr == set[i])
							{
								found = true;
								break;
							}
						}

						if (!found) return static_cast<int>(ptr - _buffer);
						--ptr;
					}

					// None mached, retrun the beginning of the string

					return 0;
				}

inline		basic_string	findWord(const int wordIndex, const T token = '|') const
				{
					// Searching backwards?

					if (wordIndex < 0) return rfindWord(-wordIndex, token);

					// Our result will go here

					basic_string	result;

					// No buffer, bail

					if (!_buffer || !length()) return result;

					// Find the start of the word

					int	index = wordIndex+1;
					T *	start = _buffer;
					while(--index && start)
					{
						start = fstl::strchr(start, token);
						if (start) start++;
					}

					// If the word wasn't in the string, then bail

					if (index || !start) return result;

					// Where does the word end?

					T *	end = fstl::strchr(start, token);
					if (!end) end = start + fstl::strlen(start);

					// Copy it into the new string

					result.set(start, static_cast<unsigned int>(end-start));

					// Return the result

					return result;
				}
				
inline		basic_string	rfindWord(const int wordIndex, const T token = '|') const
				{
					// Searching forwards?

					if (wordIndex < 0) return findWord(-wordIndex, token);

					// Our result will go here

					basic_string	result;

					// No buffer, bail

					if (!_buffer || !length()) return result;

					// Find the end of the word

					int	index = wordIndex;
					T *	end = &_buffer[length() - 1];
					while(end >= _buffer)
					{
						if (*end == token && !--index) break;
						end--;
					}

					// If the word wasn't in the string, then bail

					if (index) return result;

					// Where does the word start?

					T *	start = end - 1;
					while(start >= _buffer)
					{
						if (*start == token) break;
						start--;
					}
					if (start < _buffer) start = _buffer;

					// Copy it into the new string

					result.set(start, static_cast<unsigned int>(end-start));

					// Return the result

					return result;
				}

inline		void		fill(const T c = ' ')
				{
					if (!_buffer) return;
					fstl::memset(_buffer, c, length());
				}

inline		void		trimBeginning(const T * charList)
				{
					// We need SOME input

					if (!charList || !*charList || !_buffer) return;

					// How many characters to trim?

					unsigned int	count = static_cast<unsigned int>(fstl::strspn(_buffer, charList));

					// Trim none?

					if (count == 0) return;

					// Trim all?

					if (count == length())
					{
						erase();
						return;
					}

					// Trim some

					fstl::memmove(_buffer, &_buffer[count], (length() - count));
					resize(length() - count);
				}

inline		void		trimEnd(const T * charList)
				{
					// We need SOME input

					if (!charList || !*charList || !_buffer || !length()) return;

					// Find the last character in the string that is not of set charList

					T	*end = &_buffer[length()-1];
					while(end > _buffer && fstl::strchr(charList, *end)) end--;

					// Set the new string length

					resize(static_cast<unsigned int>(end-_buffer+1));
				}

inline		void		trim(const T * charList)
				{
					trimBeginning(charList);
					trimEnd(charList);
				}

inline		void		toupper()
				{
					if (!_buffer) return;
					T	*ptr = _buffer;
					for (unsigned int i = 0; i < length(); i++, ptr++) *ptr = static_cast<T>(fstl::toupper(*ptr));
				}

inline		void		tolower()
				{
					if (!_buffer) return;
					T	*ptr = _buffer;
					for (unsigned int i = 0; i < length(); i++, ptr++) *ptr = static_cast<T>(fstl::tolower(*ptr));
				}

	// Accessors

inline		unsigned int &	length()	{return _length;}
inline	const	unsigned int	length() const	{return _length;}

private:
	// Utilitarian (private)

inline		void		set(const T *str, const unsigned int len)
				{
					resize(len);
					if (!len) return;
					fstl::strncpy(_buffer, str, len);
					_buffer[length()] = 0;
				}

inline		void		resize(const unsigned int len)
				{
					// Do we need to resize?

					if (len == length()) return;

					// Are they clearing the buffer?

					if (!len)
					{
						deallocate(_buffer);
						_buffer = static_cast<T *>(0);
						_length = 0;
						return;
					}

					// Resize the buffer

					T	*temp = allocate<T>(len + 1);

					// Move the data over

					if (len > length())
					{
						fstl::memcpy(temp, _buffer, length());
						temp[length()] = 0;
					}
					else
					{
						fstl::memcpy(temp, _buffer, len);
						temp[len] = 0;
					}

					// Commit the changes

					deallocate(_buffer);
					_buffer = temp;
					length() = len;
				}

inline	const	T *		buffer() const
				{
					if (_buffer) return _buffer;
					return empty_string<T>();
				}

		// The string

		unsigned int	_length;
		T *		_buffer;
};

// ---------------------------------------------------------------------------------------------------------------------------------
// Convenience types - Most common uses
// ---------------------------------------------------------------------------------------------------------------------------------

typedef	basic_string<char>	string;
typedef	array<string>		StringArray;
typedef	list<string>		StringList;

typedef	basic_string<wchar_t>	wstring;
typedef	array<wstring>		WStringArray;
typedef	list<wstring>		WStringList;

// ---------------------------------------------------------------------------------------------------------------------------------
// Mixed-mode global overrides
// ---------------------------------------------------------------------------------------------------------------------------------

inline	string	operator +(const char * lhs, const string & rhs) {return fstl::string(lhs) + rhs;}
//inline	wstring	operator +(const wchar_t * lhs, const wstring & rhs) {return fstl::wstring(lhs) + rhs;}

// ---------------------------------------------------------------------------------------------------------------------------------
// Specializations for wide char conversion
// ---------------------------------------------------------------------------------------------------------------------------------
/*
inline	basic_string<wchar_t>::basic_string(const wchar_t * str)
	: _length(0), _buffer(static_cast<T *>(0))
{
	resize(static_cast<const unsigned int>(wcslen(str)));

	if (_buffer)
	{
		_buffer[length()] = 0;
		wcscpy(_buffer, str);
	}
}
*/
#ifdef	_UNICODE
inline	basic_string<wchar_t>::basic_string(const char * str)
	: _length(0), _buffer(static_cast<T *>(0))
{
	int	len = MultiByteToWideChar(CP_ACP, 0, str, -1, 0, 0);
	if (len) --len;
	resize(len);

	if (_buffer)
	{
		_buffer[length()] = 0;
		MultiByteToWideChar(CP_ACP, 0, str, -1, _buffer, length());
	}
}

inline	basic_string<char>::basic_string(const wchar_t * str)
	: _length(0), _buffer(static_cast<T *>(0))
{
	resize(static_cast<const unsigned int>(wcslen(str)));

	if (_buffer)
	{
		_buffer[length()] = 0;
		WideCharToMultiByte(CP_ACP, 0, str, -1, _buffer, length(), NULL, NULL);
	}
}
#endif // _UNICODE

FSTL_NAMESPACE_END
#endif // _FSTL_STRING
// ---------------------------------------------------------------------------------------------------------------------------------
// string - End of file
// ---------------------------------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -