Esempio n. 1
0
// See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
// Some tricky test cases:
// "abcdefghijklmn".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)/, "$001"): "$001"
// "abcdefghijklmn".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)/, "$01"): "a"
// "abcdefghijklmn".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)/, "$10"): "j"
// "abcdefghijklmn".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)/, "$15"): "a5"
// "abcdefghijklmn".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)/, "$20"): "b0"
void insertReplacedString(wstring& builder, const wstring& base, const wstring& str, const RegExpMatch& match)
{
	const vector<wstring>& substrings = match.substrings;
	const wstring& mstr = substrings[0];
	for (size_t i = 0; i < str.length(); i++)
	{
		if (str[i] == L'$' && str.length() > i + 1)
		{
			wchar_t ch = str[++i];
			switch (ch)
			{
			case L'$': // insert a '$'
				builder.push_back(ch);
				break;
			case L'&': // insert the matched string
				builder.append(mstr);
				break;
			case L'`': // insert the portion preceding the matched string
				builder.append(base.begin(), base.begin() + match.index);
				break;
			case L'\'': // insert the portion following the matched string
				builder.append(base.begin() + match.index + mstr.length(), base.end());
				break;
			default:
				if (ch >= L'0' && ch <= L'9')
				{
					int expidx = 0;
					wchar_t ch2 = str.length() > i + 1 ? str[i + 1] : L'\0';
					if (ch2 >= L'0' && ch2 <= L'9')
					{
						expidx = ch2 - L'0' + 10 * (ch - L'0');
						// if expidx overflows, fall back to single-digit
						if (expidx == 0 || expidx >= (int)substrings.size())
						{
							expidx = ch - L'0';
							ch2 = 0;
						}
					}
					else
					{
						ch2 = 0;
						expidx = ch - L'0';
					}
					// substrings.size() is 1 bigger than actual sub matches
					if (expidx < (int)substrings.size() && expidx > 0)
					{
						const wstring& submstr = substrings[expidx];
						builder.append(submstr);
						if (ch2) ++i;
						break;
					}
				}
				// $ escape fails, output as is
				builder.push_back(L'$');
				builder.push_back(ch);
			}
		}
		else builder.push_back(str[i]);
	}
}
Esempio n. 2
0
 bool JstrChecker::IS_STRING(const wstring &str, wstring &strvl) {
     if (str.front() != JsonSymbol::STRING_BOUND || str.back() != JsonSymbol::STRING_BOUND) return false;
     strvl = L"";
     for (size_t i = 1; i < str.size() - 1; i++) {
         if (str[i] == JsonSymbol::ESCAPE_SYM) {
             if (i == str.size() - 2) return false;
             strvl.push_back(ESCAPE(str[i + 1]));
             i++;
         } else if (str[i] == JsonSymbol::STRING_BOUND) {
             return false;
         } else {
             strvl.push_back(str[i]);
         }
     }
     return true;
 }
bool CItem::x_GetAttribValue(LPCTSTR& pItem , wstring& wsValue)
{
	// pItem 为 "'values' more..."
	while ( ' ' == *pItem ) pItem++;
	if ( '\'' != *pItem++ )
		return false ;

	while ( '\0' != *pItem && '\'' != *pItem ) wsValue.push_back(*pItem++) ;

	return '\'' == *pItem ;
}
Esempio n. 4
0
static void do_log(int log_level, const char *msg, va_list args, void *param)
{
	fstream &logFile = *static_cast<fstream*>(param);
	char str[4096];

#ifndef _WIN32
	va_list args2;
	va_copy(args2, args);
#endif

	vsnprintf(str, 4095, msg, args);

#ifdef _WIN32
	if (IsDebuggerPresent()) {
		int wNum = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
		if (wNum > 1) {
			static wstring wide_buf;
			static mutex wide_mutex;

			lock_guard<mutex> lock(wide_mutex);
			wide_buf.reserve(wNum + 1);
			wide_buf.resize(wNum - 1);
			MultiByteToWideChar(CP_UTF8, 0, str, -1, &wide_buf[0],
					wNum);
			wide_buf.push_back('\n');

			OutputDebugStringW(wide_buf.c_str());
		}
	}
#else
	def_log_handler(log_level, msg, args2, nullptr);
#endif

	if (too_many_repeated_entries(logFile, msg, str))
		return;

	if (log_level <= LOG_INFO || log_verbose)
		LogStringChunk(logFile, str);

#if defined(_WIN32) && defined(OBS_DEBUGBREAK_ON_ERROR)
	if (log_level <= LOG_ERROR && IsDebuggerPresent())
		__debugbreak();
#endif
}
Esempio n. 5
0
static bool utf8_to_wstring(wstring& dest, const string& src) {
	bool err = false;
	dest.clear();
	wchar_t w = 0;
	int bytes = 0;
	const wchar_t errChar = L'�';
	for (size_t i = 0; i < src.size(); i++) {
		unsigned char c = (unsigned char) src[i];
		if (c <= 0x7f) {//first byte
			if (bytes) {
				dest.push_back(errChar);
				bytes = 0;
				err = true;
			}
			dest.push_back((wchar_t) c);
		} else if (c <= 0xbf) {//second/third/etc byte
			if (bytes) {
				w = ((w << 6) | (c & 0x3f));
				bytes--;
				if (bytes == 0)
					dest.push_back(w);
			} else {
				dest.push_back(errChar);
				err = true;
			}
		} else if (c <= 0xdf) {
			//2byte sequence start
			bytes = 1;
			w = c & 0x1f;
		} else if (c <= 0xef) {//3byte sequence start
			bytes = 2;
			w = c & 0x0f;
		} else if (c <= 0xf7) {//3byte sequence start
			bytes = 3;
			w = c & 0x07;
		} else {
			dest.push_back(errChar);
			bytes = 0;
			err = true;
		}
	}
	if (bytes) {
		dest.push_back(errChar);
		err = true;
	}
	return err;
}