2006-11-28 14:19:18 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file string.cpp Handling of C-type strings (char*). */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
#include "stdafx.h"
|
2007-07-14 22:37:40 +00:00
|
|
|
#include "debug.h"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2008-10-28 14:42:31 +00:00
|
|
|
#include "core/math_func.hpp"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2013-08-05 20:35:27 +00:00
|
|
|
#include "string_base.h"
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/control_codes.h"
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
#include <stdarg.h>
|
2009-03-15 00:32:18 +00:00
|
|
|
#include <ctype.h> /* required for tolower() */
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2010-02-17 15:07:09 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <errno.h> // required by vsnprintf implementation for MSVC
|
|
|
|
#endif
|
|
|
|
|
2010-11-27 22:47:29 +00:00
|
|
|
#ifdef WITH_ICU
|
|
|
|
/* Required by strnatcmp. */
|
|
|
|
#include <unicode/ustring.h>
|
|
|
|
#include "language.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#endif /* WITH_ICU */
|
|
|
|
|
2008-10-28 15:47:42 +00:00
|
|
|
/**
|
|
|
|
* Safer implementation of vsnprintf; same as vsnprintf except:
|
|
|
|
* - last instead of size, i.e. replace sizeof with lastof.
|
|
|
|
* - return gives the amount of characters added, not what it would add.
|
|
|
|
* @param str buffer to write to up to last
|
|
|
|
* @param last last character we may write to
|
|
|
|
* @param format the formatting (see snprintf)
|
|
|
|
* @param ap the list of arguments for the format
|
|
|
|
* @return the number of added characters
|
|
|
|
*/
|
|
|
|
static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
|
|
|
|
{
|
2010-02-17 13:55:08 +00:00
|
|
|
ptrdiff_t diff = last - str;
|
|
|
|
if (diff < 0) return 0;
|
|
|
|
return min((int)diff, vsnprintf(str, diff + 1, format, ap));
|
2008-10-28 15:47:42 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Appends characters from one string to another.
|
|
|
|
*
|
|
|
|
* Appends the source string to the destination string with respect of the
|
|
|
|
* terminating null-character and the maximum size of the destination
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* @note usage ttd_strlcat(dst, src, lengthof(dst));
|
|
|
|
* @note lengthof() applies only to fixed size arrays
|
|
|
|
*
|
|
|
|
* @param dst The buffer containing the target string
|
|
|
|
* @param src The buffer containing the string to append
|
|
|
|
* @param size The maximum size of the destination buffer
|
|
|
|
*/
|
2006-11-28 14:19:18 +00:00
|
|
|
void ttd_strlcat(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
2008-10-29 16:30:41 +00:00
|
|
|
while (size > 0 && *dst != '\0') {
|
|
|
|
size--;
|
|
|
|
dst++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ttd_strlcpy(dst, src, size);
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Copies characters from one buffer to another.
|
|
|
|
*
|
|
|
|
* Copies the source string to the destination buffer with respect of the
|
|
|
|
* terminating null-character and the maximum size of the destination
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* @note usage ttd_strlcpy(dst, src, lengthof(dst));
|
|
|
|
* @note lengthof() applies only to fixed size arrays
|
|
|
|
*
|
|
|
|
* @param dst The destination buffer
|
|
|
|
* @param src The buffer containing the string to copy
|
|
|
|
* @param size The maximum size of the destination buffer
|
|
|
|
*/
|
2006-11-28 14:19:18 +00:00
|
|
|
void ttd_strlcpy(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
2008-10-29 16:30:41 +00:00
|
|
|
while (--size > 0 && *src != '\0') {
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
2006-11-28 14:19:18 +00:00
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Appends characters from one string to another.
|
|
|
|
*
|
|
|
|
* Appends the source string to the destination string with respect of the
|
|
|
|
* terminating null-character and and the last pointer to the last element
|
|
|
|
* in the destination buffer. If the last pointer is set to NULL no
|
|
|
|
* boundary check is performed.
|
|
|
|
*
|
|
|
|
* @note usage: strecat(dst, src, lastof(dst));
|
|
|
|
* @note lastof() applies only to fixed size arrays
|
|
|
|
*
|
|
|
|
* @param dst The buffer containing the target string
|
|
|
|
* @param src The buffer containing the string to append
|
|
|
|
* @param last The pointer to the last element of the destination buffer
|
|
|
|
* @return The pointer to the terminating null-character in the destination buffer
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
char *strecat(char *dst, const char *src, const char *last)
|
2006-11-28 14:19:18 +00:00
|
|
|
{
|
|
|
|
assert(dst <= last);
|
2008-10-29 16:30:41 +00:00
|
|
|
while (*dst != '\0') {
|
2006-11-28 14:19:18 +00:00
|
|
|
if (dst == last) return dst;
|
2008-10-29 16:30:41 +00:00
|
|
|
dst++;
|
|
|
|
}
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
return strecpy(dst, src, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Copies characters from one buffer to another.
|
|
|
|
*
|
|
|
|
* Copies the source string to the destination buffer with respect of the
|
|
|
|
* terminating null-character and the last pointer to the last element in
|
|
|
|
* the destination buffer. If the last pointer is set to NULL no boundary
|
|
|
|
* check is performed.
|
|
|
|
*
|
|
|
|
* @note usage: strecpy(dst, src, lastof(dst));
|
|
|
|
* @note lastof() applies only to fixed size arrays
|
|
|
|
*
|
|
|
|
* @param dst The destination buffer
|
|
|
|
* @param src The buffer containing the string to copy
|
|
|
|
* @param last The pointer to the last element of the destination buffer
|
|
|
|
* @return The pointer to the terminating null-character in the destination buffer
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
char *strecpy(char *dst, const char *src, const char *last)
|
2006-11-28 14:19:18 +00:00
|
|
|
{
|
|
|
|
assert(dst <= last);
|
2008-10-29 16:30:41 +00:00
|
|
|
while (dst != last && *src != '\0') {
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
2006-11-28 14:19:18 +00:00
|
|
|
*dst = '\0';
|
2008-10-29 16:30:41 +00:00
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
if (dst == last && *src != '\0') {
|
2011-03-03 21:43:24 +00:00
|
|
|
#if defined(STRGEN) || defined(SETTINGSGEN)
|
2006-11-28 14:19:18 +00:00
|
|
|
error("String too long for destination buffer");
|
2011-03-03 21:43:24 +00:00
|
|
|
#else /* STRGEN || SETTINGSGEN */
|
2007-07-14 22:37:40 +00:00
|
|
|
DEBUG(misc, 0, "String too long for destination buffer");
|
2011-03-03 21:43:24 +00:00
|
|
|
#endif /* STRGEN || SETTINGSGEN */
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Format, "printf", into a newly allocated string.
|
|
|
|
* @param str The formatting string.
|
|
|
|
* @return The formatted string. You must free this!
|
|
|
|
*/
|
2008-10-28 14:42:31 +00:00
|
|
|
char *CDECL str_fmt(const char *str, ...)
|
2006-11-28 14:19:18 +00:00
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, str);
|
2008-10-28 14:42:31 +00:00
|
|
|
int len = vseprintf(buf, lastof(buf), str, va);
|
2006-11-28 14:19:18 +00:00
|
|
|
va_end(va);
|
2008-10-28 14:42:31 +00:00
|
|
|
char *p = MallocT<char>(len + 1);
|
|
|
|
memcpy(p, buf, len + 1);
|
2006-11-28 14:19:18 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:29:25 +00:00
|
|
|
/**
|
|
|
|
* Scan the string for old values of SCC_ENCODED and fix it to
|
|
|
|
* it's new, static value.
|
|
|
|
* @param str the string to scan
|
|
|
|
* @param last the last valid character of str
|
|
|
|
*/
|
|
|
|
void str_fix_scc_encoded(char *str, const char *last)
|
|
|
|
{
|
|
|
|
while (str <= last && *str != '\0') {
|
|
|
|
size_t len = Utf8EncodedCharLen(*str);
|
|
|
|
if ((len == 0 && str + 4 > last) || str + len > last) break;
|
|
|
|
|
|
|
|
WChar c;
|
|
|
|
len = Utf8Decode(&c, str);
|
|
|
|
if (c == '\0') break;
|
|
|
|
|
|
|
|
if (c == 0xE028 || c == 0xE02A) {
|
|
|
|
c = SCC_ENCODED;
|
|
|
|
}
|
|
|
|
str += Utf8Encode(str, c);
|
|
|
|
}
|
|
|
|
*str = '\0';
|
|
|
|
}
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Scans the string for valid characters and if it finds invalid ones,
|
|
|
|
* replaces them with a question mark '?' (if not ignored)
|
|
|
|
* @param str the string to validate
|
|
|
|
* @param last the last valid character of str
|
2011-12-18 18:37:54 +00:00
|
|
|
* @param settings the settings for the string validation.
|
2011-01-18 23:09:43 +00:00
|
|
|
*/
|
2011-12-18 18:37:54 +00:00
|
|
|
void str_validate(char *str, const char *last, StringValidationSettings settings)
|
2006-11-28 14:19:18 +00:00
|
|
|
{
|
2009-03-06 01:23:25 +00:00
|
|
|
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
char *dst = str;
|
2010-07-25 23:14:59 +00:00
|
|
|
while (str <= last && *str != '\0') {
|
2009-03-06 01:23:25 +00:00
|
|
|
size_t len = Utf8EncodedCharLen(*str);
|
|
|
|
/* If the character is unknown, i.e. encoded length is 0
|
|
|
|
* we assume worst case for the length check.
|
|
|
|
* The length check is needed to prevent Utf8Decode to read
|
|
|
|
* over the terminating '\0' if that happens to be placed
|
|
|
|
* within the encoding of an UTF8 character. */
|
|
|
|
if ((len == 0 && str + 4 > last) || str + len > last) break;
|
|
|
|
|
|
|
|
WChar c;
|
|
|
|
len = Utf8Decode(&c, str);
|
|
|
|
/* It's possible to encode the string termination character
|
|
|
|
* into a multiple bytes. This prevents those termination
|
|
|
|
* characters to be skipped */
|
|
|
|
if (c == '\0') break;
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2012-01-16 22:29:25 +00:00
|
|
|
if ((IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) || ((settings & SVS_ALLOW_CONTROL_CODE) != 0 && c == SCC_ENCODED)) {
|
2006-11-28 14:19:18 +00:00
|
|
|
/* Copy the character back. Even if dst is current the same as str
|
|
|
|
* (i.e. no characters have been changed) this is quicker than
|
|
|
|
* moving the pointers ahead by len */
|
|
|
|
do {
|
|
|
|
*dst++ = *str++;
|
|
|
|
} while (--len != 0);
|
2011-12-18 18:37:54 +00:00
|
|
|
} else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') {
|
2009-01-18 13:12:57 +00:00
|
|
|
*dst++ = *str++;
|
2006-11-28 14:19:18 +00:00
|
|
|
} else {
|
2011-12-18 18:37:54 +00:00
|
|
|
if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
|
2009-01-18 13:12:57 +00:00
|
|
|
str += len;
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-28 14:19:18 +00:00
|
|
|
/* Replace the undesirable character with a question mark */
|
|
|
|
str += len;
|
2011-12-18 18:37:54 +00:00
|
|
|
if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?';
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
|
2013-07-04 16:36:47 +00:00
|
|
|
/**
|
|
|
|
* Scans the string for valid characters and if it finds invalid ones,
|
|
|
|
* replaces them with a question mark '?'.
|
|
|
|
* @param str the string to validate
|
|
|
|
*/
|
|
|
|
void ValidateString(const char *str)
|
|
|
|
{
|
|
|
|
/* We know it is '\0' terminated. */
|
|
|
|
str_validate(const_cast<char *>(str), str + strlen(str) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the given string is valid, i.e. contains only
|
|
|
|
* valid (printable) characters and is properly terminated.
|
|
|
|
* @param str The string to validate.
|
|
|
|
* @param last The last character of the string, i.e. the string
|
|
|
|
* must be terminated here or earlier.
|
|
|
|
*/
|
2010-11-10 17:49:14 +00:00
|
|
|
bool StrValid(const char *str, const char *last)
|
|
|
|
{
|
|
|
|
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
|
|
|
|
|
|
|
|
while (str <= last && *str != '\0') {
|
|
|
|
size_t len = Utf8EncodedCharLen(*str);
|
|
|
|
/* Encoded length is 0 if the character isn't known.
|
|
|
|
* The length check is needed to prevent Utf8Decode to read
|
|
|
|
* over the terminating '\0' if that happens to be placed
|
|
|
|
* within the encoding of an UTF8 character. */
|
|
|
|
if (len == 0 || str + len > last) return false;
|
|
|
|
|
|
|
|
WChar c;
|
|
|
|
len = Utf8Decode(&c, str);
|
|
|
|
if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *str == '\0';
|
|
|
|
}
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/** Scans the string for colour codes and strips them */
|
2006-11-28 14:19:18 +00:00
|
|
|
void str_strip_colours(char *str)
|
|
|
|
{
|
|
|
|
char *dst = str;
|
|
|
|
WChar c;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
|
|
|
|
if (c < SCC_BLUE || c > SCC_BLACK) {
|
|
|
|
/* Copy the character back. Even if dst is current the same as str
|
|
|
|
* (i.e. no characters have been changed) this is quicker than
|
|
|
|
* moving the pointers ahead by len */
|
|
|
|
do {
|
|
|
|
*dst++ = *str++;
|
|
|
|
} while (--len != 0);
|
|
|
|
} else {
|
|
|
|
/* Just skip (strip) the colour codes */
|
|
|
|
str += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
|
2010-12-05 22:17:25 +00:00
|
|
|
/**
|
|
|
|
* Get the length of an UTF-8 encoded string in number of characters
|
|
|
|
* and thus not the number of bytes that the encoded string contains.
|
|
|
|
* @param s The string to get the length for.
|
|
|
|
* @return The length of the string in characters.
|
|
|
|
*/
|
|
|
|
size_t Utf8StringLength(const char *s)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
const char *t = s;
|
|
|
|
while (Utf8Consume(&t) != 0) len++;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Convert a given ASCII string to lowercase.
|
2006-11-28 14:19:18 +00:00
|
|
|
* NOTE: only support ASCII characters, no UTF8 fancy. As currently
|
|
|
|
* the function is only used to lowercase data-filenames if they are
|
|
|
|
* not found, this is sufficient. If more, or general functionality is
|
|
|
|
* needed, look to r7271 where it was removed because it was broken when
|
|
|
|
* using certain locales: eg in Turkish the uppercase 'I' was converted to
|
2007-04-04 01:35:16 +00:00
|
|
|
* '?', so just revert to the old functionality
|
2010-08-01 19:44:49 +00:00
|
|
|
* @param str string to convert
|
2011-07-16 18:43:22 +00:00
|
|
|
* @return String has changed.
|
2010-08-01 19:44:49 +00:00
|
|
|
*/
|
2011-07-16 18:43:22 +00:00
|
|
|
bool strtolower(char *str)
|
2006-11-28 14:19:18 +00:00
|
|
|
{
|
2011-07-16 18:43:22 +00:00
|
|
|
bool changed = false;
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
char new_str = tolower(*str);
|
|
|
|
changed |= new_str != *str;
|
|
|
|
*str = new_str;
|
|
|
|
}
|
|
|
|
return changed;
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only allow certain keys. You can define the filter to be used. This makes
|
|
|
|
* sure no invalid keys can get into an editbox, like BELL.
|
|
|
|
* @param key character to be checked
|
|
|
|
* @param afilter the filter to use
|
|
|
|
* @return true or false depending if the character is printable/valid or not
|
|
|
|
*/
|
|
|
|
bool IsValidChar(WChar key, CharSetFilter afilter)
|
|
|
|
{
|
|
|
|
switch (afilter) {
|
2009-10-31 14:06:16 +00:00
|
|
|
case CS_ALPHANUMERAL: return IsPrintable(key);
|
|
|
|
case CS_NUMERAL: return (key >= '0' && key <= '9');
|
|
|
|
case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
|
|
|
|
case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
|
2010-04-24 07:31:58 +00:00
|
|
|
case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-01-02 18:42:51 +00:00
|
|
|
/* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
|
|
|
|
#if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
|
2006-11-28 14:19:18 +00:00
|
|
|
int CDECL snprintf(char *str, size_t size, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
ret = vsnprintf(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
2008-01-02 18:42:51 +00:00
|
|
|
#endif /* MinGW Runtime < 3.14 */
|
2006-11-28 14:19:18 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2010-02-17 15:07:09 +00:00
|
|
|
/**
|
|
|
|
* Almost POSIX compliant implementation of \c vsnprintf for VC compiler.
|
|
|
|
* The difference is in the value returned on output truncation. This
|
|
|
|
* implementation returns size whereas a POSIX implementation returns
|
|
|
|
* size or more (the number of bytes that would be written to str
|
|
|
|
* had size been sufficiently large excluding the terminating null byte).
|
2007-01-13 13:06:18 +00:00
|
|
|
*/
|
2006-11-28 14:19:18 +00:00
|
|
|
int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
|
|
|
{
|
2010-02-17 15:07:09 +00:00
|
|
|
if (size == 0) return 0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
int ret = _vsnprintf(str, size, format, ap);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
/* There's a formatting error, better get that looked
|
|
|
|
* at properly instead of ignoring it. */
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
} else if ((size_t)ret < size) {
|
|
|
|
/* The buffer is big enough for the number of
|
2013-01-08 22:46:42 +00:00
|
|
|
* characters stored (excluding null), i.e.
|
2010-02-17 15:07:09 +00:00
|
|
|
* the string has been null-terminated. */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The buffer is too small for _vsnprintf to write the
|
|
|
|
* null-terminator at its end and return size. */
|
|
|
|
str[size - 1] = '\0';
|
2010-02-18 01:10:04 +00:00
|
|
|
return (int)size;
|
2006-11-28 14:19:18 +00:00
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
2008-10-28 14:42:31 +00:00
|
|
|
/**
|
|
|
|
* Safer implementation of snprintf; same as snprintf except:
|
|
|
|
* - last instead of size, i.e. replace sizeof with lastof.
|
|
|
|
* - return gives the amount of characters added, not what it would add.
|
|
|
|
* @param str buffer to write to up to last
|
|
|
|
* @param last last character we may write to
|
|
|
|
* @param format the formatting (see snprintf)
|
|
|
|
* @return the number of added characters
|
|
|
|
*/
|
|
|
|
int CDECL seprintf(char *str, const char *last, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
int ret = vseprintf(str, last, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Convert the md5sum to a hexadecimal string representation
|
2007-01-13 13:47:57 +00:00
|
|
|
* @param buf buffer to put the md5sum into
|
|
|
|
* @param last last character of buffer (usually lastof(buf))
|
|
|
|
* @param md5sum the md5sum itself
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return a pointer to the next character after the md5sum
|
|
|
|
*/
|
2007-01-13 13:47:57 +00:00
|
|
|
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
for (uint i = 0; i < 16; i++) {
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "%02X", md5sum[i]);
|
2007-01-13 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-28 14:19:18 +00:00
|
|
|
/* UTF-8 handling routines */
|
|
|
|
|
|
|
|
|
2010-06-27 09:05:10 +00:00
|
|
|
/**
|
|
|
|
* Decode and consume the next UTF-8 encoded character.
|
2006-11-28 14:19:18 +00:00
|
|
|
* @param c Buffer to place decoded character.
|
|
|
|
* @param s Character stream to retrieve character from.
|
|
|
|
* @return Number of characters in the sequence.
|
|
|
|
*/
|
|
|
|
size_t Utf8Decode(WChar *c, const char *s)
|
|
|
|
{
|
|
|
|
assert(c != NULL);
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(s[0], 7)) {
|
2006-11-28 14:19:18 +00:00
|
|
|
/* Single byte character: 0xxxxxxx */
|
|
|
|
*c = s[0];
|
|
|
|
return 1;
|
|
|
|
} else if (GB(s[0], 5, 3) == 6) {
|
|
|
|
if (IsUtf8Part(s[1])) {
|
|
|
|
/* Double byte character: 110xxxxx 10xxxxxx */
|
|
|
|
*c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
|
|
|
|
if (*c >= 0x80) return 2;
|
|
|
|
}
|
|
|
|
} else if (GB(s[0], 4, 4) == 14) {
|
|
|
|
if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
|
|
|
|
/* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
|
|
|
|
*c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
|
|
|
|
if (*c >= 0x800) return 3;
|
|
|
|
}
|
|
|
|
} else if (GB(s[0], 3, 5) == 30) {
|
|
|
|
if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
|
|
|
|
/* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
|
|
|
|
*c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
|
|
|
|
if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
|
2006-11-28 14:19:18 +00:00
|
|
|
*c = '?';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-27 09:05:10 +00:00
|
|
|
/**
|
|
|
|
* Encode a unicode character and place it in the buffer.
|
2006-11-28 14:19:18 +00:00
|
|
|
* @param buf Buffer to place character.
|
|
|
|
* @param c Unicode character to encode.
|
|
|
|
* @return Number of characters in the encoded sequence.
|
|
|
|
*/
|
|
|
|
size_t Utf8Encode(char *buf, WChar c)
|
|
|
|
{
|
|
|
|
if (c < 0x80) {
|
|
|
|
*buf = c;
|
|
|
|
return 1;
|
|
|
|
} else if (c < 0x800) {
|
|
|
|
*buf++ = 0xC0 + GB(c, 6, 5);
|
|
|
|
*buf = 0x80 + GB(c, 0, 6);
|
|
|
|
return 2;
|
|
|
|
} else if (c < 0x10000) {
|
|
|
|
*buf++ = 0xE0 + GB(c, 12, 4);
|
|
|
|
*buf++ = 0x80 + GB(c, 6, 6);
|
|
|
|
*buf = 0x80 + GB(c, 0, 6);
|
|
|
|
return 3;
|
|
|
|
} else if (c < 0x110000) {
|
|
|
|
*buf++ = 0xF0 + GB(c, 18, 3);
|
|
|
|
*buf++ = 0x80 + GB(c, 12, 6);
|
|
|
|
*buf++ = 0x80 + GB(c, 6, 6);
|
|
|
|
*buf = 0x80 + GB(c, 0, 6);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
|
2006-11-28 14:19:18 +00:00
|
|
|
*buf = '?';
|
|
|
|
return 1;
|
|
|
|
}
|
2007-03-05 00:45:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Properly terminate an UTF8 string to some maximum length
|
|
|
|
* @param s string to check if it needs additional trimming
|
|
|
|
* @param maxlen the maximum length the buffer can have.
|
|
|
|
* @return the new length in bytes of the string (eg. strlen(new_string))
|
2012-01-01 17:36:19 +00:00
|
|
|
* @note maxlen is the string length _INCLUDING_ the terminating '\0'
|
2007-03-05 00:45:56 +00:00
|
|
|
*/
|
|
|
|
size_t Utf8TrimString(char *s, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
|
|
|
|
size_t len = Utf8EncodedCharLen(*s);
|
2007-03-10 00:26:19 +00:00
|
|
|
/* Silently ignore invalid UTF8 sequences, our only concern trimming */
|
|
|
|
if (len == 0) len = 1;
|
2007-03-05 00:45:56 +00:00
|
|
|
|
|
|
|
/* Take care when a hard cutoff was made for the string and
|
|
|
|
* the last UTF8 sequence is invalid */
|
|
|
|
if (length + len >= maxlen || (s + len > ptr)) break;
|
|
|
|
s += len;
|
|
|
|
length += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
return length;
|
2007-03-05 05:03:28 +00:00
|
|
|
}
|
2008-08-24 17:02:21 +00:00
|
|
|
|
2010-05-21 15:56:19 +00:00
|
|
|
#ifdef DEFINE_STRNDUP
|
2008-08-24 17:02:21 +00:00
|
|
|
char *strndup(const char *s, size_t len)
|
|
|
|
{
|
2013-06-30 08:29:51 +00:00
|
|
|
len = ttd_strnlen(s, len);
|
2008-08-24 17:02:21 +00:00
|
|
|
char *tmp = CallocT<char>(len + 1);
|
|
|
|
memcpy(tmp, s, len);
|
|
|
|
return tmp;
|
|
|
|
}
|
2010-05-21 15:56:19 +00:00
|
|
|
#endif /* DEFINE_STRNDUP */
|
2009-02-06 11:58:52 +00:00
|
|
|
|
2009-02-06 15:53:48 +00:00
|
|
|
#ifdef DEFINE_STRCASESTR
|
2010-01-26 14:04:56 +00:00
|
|
|
char *strcasestr(const char *haystack, const char *needle)
|
2009-02-06 11:58:52 +00:00
|
|
|
{
|
|
|
|
size_t hay_len = strlen(haystack);
|
|
|
|
size_t needle_len = strlen(needle);
|
|
|
|
while (hay_len >= needle_len) {
|
2010-01-26 14:04:56 +00:00
|
|
|
if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
|
2009-02-06 11:58:52 +00:00
|
|
|
|
|
|
|
haystack++;
|
|
|
|
hay_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-06 15:53:48 +00:00
|
|
|
#endif /* DEFINE_STRCASESTR */
|
2010-11-27 22:47:29 +00:00
|
|
|
|
2013-02-09 17:31:07 +00:00
|
|
|
/**
|
|
|
|
* Skip some of the 'garbage' in the string that we don't want to use
|
|
|
|
* to sort on. This way the alphabetical sorting will work better as
|
|
|
|
* we would be actually using those characters instead of some other
|
|
|
|
* characters such as spaces and tildes at the begin of the name.
|
|
|
|
* @param str The string to skip the initial garbage of.
|
|
|
|
* @return The string with the garbage skipped.
|
|
|
|
*/
|
|
|
|
static const char *SkipGarbage(const char *str)
|
|
|
|
{
|
|
|
|
while (*str != '\0' && (*str < 'A' || IsInsideMM(*str, '[', '`' + 1) || IsInsideMM(*str, '{', '~' + 1))) str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-11-27 22:47:29 +00:00
|
|
|
/**
|
|
|
|
* Compares two strings using case insensitive natural sort.
|
|
|
|
*
|
|
|
|
* @param s1 First string to compare.
|
|
|
|
* @param s2 Second string to compare.
|
2013-02-09 17:31:07 +00:00
|
|
|
* @param ignore_garbage_at_front Skip punctuation characters in the front
|
2010-11-27 22:47:29 +00:00
|
|
|
* @return Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.
|
|
|
|
*/
|
2013-02-09 17:31:07 +00:00
|
|
|
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
|
2010-11-27 22:47:29 +00:00
|
|
|
{
|
2013-02-09 17:31:07 +00:00
|
|
|
if (ignore_garbage_at_front) {
|
|
|
|
s1 = SkipGarbage(s1);
|
|
|
|
s2 = SkipGarbage(s2);
|
|
|
|
}
|
2010-11-27 22:47:29 +00:00
|
|
|
#ifdef WITH_ICU
|
|
|
|
if (_current_collator != NULL) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* We want to use the new faster method for ICU 4.2 and higher. */
|
|
|
|
#if U_ICU_VERSION_MAJOR_NUM > 4 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM >= 2)
|
|
|
|
/* The StringPiece parameter gets implicitly constructed from the char *. */
|
|
|
|
result = _current_collator->compareUTF8(s1, s2, status);
|
|
|
|
#else /* The following for 4.0 and lower. */
|
|
|
|
UChar buffer1[DRAW_STRING_BUFFER];
|
|
|
|
u_strFromUTF8Lenient(buffer1, lengthof(buffer1), NULL, s1, -1, &status);
|
|
|
|
UChar buffer2[DRAW_STRING_BUFFER];
|
|
|
|
u_strFromUTF8Lenient(buffer2, lengthof(buffer2), NULL, s2, -1, &status);
|
|
|
|
|
|
|
|
result = _current_collator->compare(buffer1, buffer2, status);
|
|
|
|
#endif /* ICU version check. */
|
|
|
|
if (U_SUCCESS(status)) return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_ICU */
|
|
|
|
|
|
|
|
/* Do a normal comparison if ICU is missing or if we cannot create a collator. */
|
|
|
|
return strcasecmp(s1, s2);
|
|
|
|
}
|
2013-08-05 20:35:27 +00:00
|
|
|
|
|
|
|
#ifdef WITH_ICU
|
|
|
|
|
|
|
|
#include <unicode/utext.h>
|
|
|
|
#include <unicode/brkiter.h>
|
|
|
|
|
|
|
|
/** String iterator using ICU as a backend. */
|
|
|
|
class IcuStringIterator : public StringIterator
|
|
|
|
{
|
|
|
|
icu::BreakIterator *char_itr; ///< ICU iterator for characters.
|
2013-08-05 20:35:31 +00:00
|
|
|
icu::BreakIterator *word_itr; ///< ICU iterator for words.
|
2013-08-05 20:35:27 +00:00
|
|
|
const char *string; ///< Iteration string in UTF-8.
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
SmallVector<UChar, 32> utf16_str; ///< UTF-16 copy of the string.
|
|
|
|
SmallVector<size_t, 32> utf16_to_utf8; ///< Mapping from UTF-16 code point position to index in the UTF-8 source string.
|
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
public:
|
2013-08-05 20:35:31 +00:00
|
|
|
IcuStringIterator() : char_itr(NULL), word_itr(NULL)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
this->char_itr = icu::BreakIterator::createCharacterInstance(icu::Locale(_current_language != NULL ? _current_language->isocode : "en"), status);
|
2013-08-05 20:35:31 +00:00
|
|
|
this->word_itr = icu::BreakIterator::createWordInstance(icu::Locale(_current_language != NULL ? _current_language->isocode : "en"), status);
|
|
|
|
|
|
|
|
*this->utf16_str.Append() = '\0';
|
|
|
|
*this->utf16_to_utf8.Append() = 0;
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~IcuStringIterator()
|
|
|
|
{
|
|
|
|
delete this->char_itr;
|
2013-08-05 20:35:31 +00:00
|
|
|
delete this->word_itr;
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetString(const char *s)
|
|
|
|
{
|
|
|
|
this->string = s;
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
/* Unfortunately current ICU versions only provide rudimentary support
|
|
|
|
* for word break iterators (especially for CJK languages) in combination
|
|
|
|
* with UTF-8 input. As a work around we have to convert the input to
|
|
|
|
* UTF-16 and create a mapping back to UTF-8 character indices. */
|
|
|
|
this->utf16_str.Clear();
|
|
|
|
this->utf16_to_utf8.Clear();
|
|
|
|
|
|
|
|
while (*s != '\0') {
|
|
|
|
size_t idx = s - this->string;
|
|
|
|
|
|
|
|
WChar c = Utf8Consume(&s);
|
|
|
|
if (c < 0x10000) {
|
|
|
|
*this->utf16_str.Append() = (UChar)c;
|
|
|
|
} else {
|
|
|
|
/* Make a surrogate pair. */
|
|
|
|
*this->utf16_str.Append() = (UChar)(0xD800 + ((c - 0x10000) >> 10));
|
|
|
|
*this->utf16_str.Append() = (UChar)(0xDC00 + ((c - 0x10000) & 0x3FF));
|
|
|
|
*this->utf16_to_utf8.Append() = idx;
|
|
|
|
}
|
|
|
|
*this->utf16_to_utf8.Append() = idx;
|
|
|
|
}
|
|
|
|
*this->utf16_str.Append() = '\0';
|
|
|
|
*this->utf16_to_utf8.Append() = s - this->string;
|
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
UText text = UTEXT_INITIALIZER;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2013-08-05 20:35:31 +00:00
|
|
|
utext_openUChars(&text, this->utf16_str.Begin(), this->utf16_str.Length() - 1, &status);
|
2013-08-05 20:35:27 +00:00
|
|
|
this->char_itr->setText(&text, status);
|
2013-08-05 20:35:31 +00:00
|
|
|
this->word_itr->setText(&text, status);
|
2013-08-05 20:35:27 +00:00
|
|
|
this->char_itr->first();
|
2013-08-05 20:35:31 +00:00
|
|
|
this->word_itr->first();
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SetCurPosition(size_t pos)
|
|
|
|
{
|
2013-08-05 20:35:31 +00:00
|
|
|
/* Convert incoming position to an UTF-16 string index. */
|
|
|
|
uint utf16_pos = 0;
|
|
|
|
for (uint i = 0; i < this->utf16_to_utf8.Length(); i++) {
|
|
|
|
if (this->utf16_to_utf8[i] == pos) {
|
|
|
|
utf16_pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
/* isBoundary has the documented side-effect of setting the current
|
|
|
|
* position to the first valid boundary equal to or greater than
|
|
|
|
* the passed value. */
|
2013-08-05 20:35:31 +00:00
|
|
|
this->char_itr->isBoundary(utf16_pos);
|
|
|
|
return this->utf16_to_utf8[this->char_itr->current()];
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
virtual size_t Next(IterType what)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
2013-08-05 20:35:31 +00:00
|
|
|
int32_t pos;
|
|
|
|
switch (what) {
|
|
|
|
case ITER_CHARACTER:
|
|
|
|
pos = this->char_itr->next();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ITER_WORD:
|
|
|
|
pos = this->word_itr->following(this->char_itr->current());
|
|
|
|
/* The ICU word iterator considers both the start and the end of a word a valid
|
|
|
|
* break point, but we only want word starts. Move to the next location in
|
|
|
|
* case the new position points to whitespace. */
|
|
|
|
while (pos != icu::BreakIterator::DONE && IsWhitespace(Utf16DecodeChar((const uint16 *)&this->utf16_str[pos]))) pos = this->word_itr->next();
|
|
|
|
|
|
|
|
this->char_itr->isBoundary(pos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
virtual size_t Prev(IterType what)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
2013-08-05 20:35:31 +00:00
|
|
|
int32_t pos;
|
|
|
|
switch (what) {
|
|
|
|
case ITER_CHARACTER:
|
|
|
|
pos = this->char_itr->previous();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ITER_WORD:
|
|
|
|
pos = this->word_itr->preceding(this->char_itr->current());
|
|
|
|
/* The ICU word iterator considers both the start and the end of a word a valid
|
|
|
|
* break point, but we only want word starts. Move to the previous location in
|
|
|
|
* case the new position points to whitespace. */
|
|
|
|
while (pos != icu::BreakIterator::DONE && IsWhitespace(Utf16DecodeChar((const uint16 *)&this->utf16_str[pos]))) pos = this->word_itr->previous();
|
|
|
|
|
|
|
|
this->char_itr->isBoundary(pos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ StringIterator *StringIterator::Create()
|
|
|
|
{
|
|
|
|
return new IcuStringIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/** Fallback simple string iterator. */
|
|
|
|
class DefaultStringIterator : public StringIterator
|
|
|
|
{
|
|
|
|
const char *string; ///< Current string.
|
|
|
|
size_t len; ///< String length.
|
|
|
|
size_t cur_pos; ///< Current iteration position.
|
|
|
|
|
|
|
|
public:
|
|
|
|
DefaultStringIterator() : string(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetString(const char *s)
|
|
|
|
{
|
|
|
|
this->string = s;
|
|
|
|
this->len = strlen(s);
|
|
|
|
this->cur_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SetCurPosition(size_t pos)
|
|
|
|
{
|
|
|
|
assert(this->string != NULL && pos <= this->len);
|
|
|
|
/* Sanitize in case we get a position inside an UTF-8 sequence. */
|
|
|
|
while (pos > 0 && IsUtf8Part(this->string[pos])) pos--;
|
|
|
|
return this->cur_pos = pos;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
virtual size_t Next(IterType what)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
|
|
|
assert(this->string != NULL);
|
|
|
|
|
|
|
|
/* Already at the end? */
|
|
|
|
if (this->cur_pos >= this->len) return END;
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
switch (what) {
|
|
|
|
case ITER_CHARACTER: {
|
|
|
|
WChar c;
|
|
|
|
this->cur_pos += Utf8Decode(&c, this->string + this->cur_pos);
|
|
|
|
return this->cur_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ITER_WORD: {
|
|
|
|
WChar c;
|
|
|
|
/* Consume current word. */
|
|
|
|
size_t offs = Utf8Decode(&c, this->string + this->cur_pos);
|
|
|
|
while (this->cur_pos < this->len && !IsWhitespace(c)) {
|
|
|
|
this->cur_pos += offs;
|
|
|
|
offs = Utf8Decode(&c, this->string + this->cur_pos);
|
|
|
|
}
|
|
|
|
/* Consume whitespace to the next word. */
|
|
|
|
while (this->cur_pos < this->len && IsWhitespace(c)) {
|
|
|
|
this->cur_pos += offs;
|
|
|
|
offs = Utf8Decode(&c, this->string + this->cur_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->cur_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return END;
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
virtual size_t Prev(IterType what)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
|
|
|
assert(this->string != NULL);
|
|
|
|
|
|
|
|
/* Already at the beginning? */
|
|
|
|
if (this->cur_pos == 0) return END;
|
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
switch (what) {
|
|
|
|
case ITER_CHARACTER:
|
|
|
|
return this->cur_pos = Utf8PrevChar(this->string + this->cur_pos) - this->string;
|
|
|
|
|
|
|
|
case ITER_WORD: {
|
|
|
|
const char *s = this->string + this->cur_pos;
|
|
|
|
WChar c;
|
|
|
|
/* Consume preceding whitespace. */
|
|
|
|
do {
|
|
|
|
s = Utf8PrevChar(s);
|
|
|
|
Utf8Decode(&c, s);
|
|
|
|
} while (s > this->string && IsWhitespace(c));
|
|
|
|
/* Consume preceding word. */
|
|
|
|
while (s > this->string && !IsWhitespace(c)) {
|
|
|
|
s = Utf8PrevChar(s);
|
|
|
|
Utf8Decode(&c, s);
|
|
|
|
}
|
|
|
|
/* Move caret back to the beginning of the word. */
|
|
|
|
if (IsWhitespace(c)) Utf8Consume(&s);
|
|
|
|
|
|
|
|
return this->cur_pos = s - this->string;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return END;
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ StringIterator *StringIterator::Create()
|
|
|
|
{
|
|
|
|
return new DefaultStringIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|