/**************************** TemporaryManager::GetNextTemporaryName -- gets next temp name (_t1, etc.) *****************************/ STRING * TemporaryManager::GetNextTemporaryName ( Vtypes TemporaryVtype, LifetimeClass Lifetime ) { if (TemporaryVtype >= 0 && TemporaryVtype < _countof(m_CountOfTemporariesOfVtype)) { StringBuffer NameBuffer; WCHAR CharacterBuffer[32]; // VS Everett security bug #515087 NameBuffer.AppendString(VBTemporaryPrefix); if (m_ExtraPrefix) { NameBuffer.AppendString(m_ExtraPrefix); NameBuffer.AppendChar(L'$'); } NameBuffer.AppendString(s_wszVOfVtype[TemporaryVtype]); NameBuffer.AppendChar(L'$'); NameBuffer.AppendChar( (Lifetime == LifetimeLongLived) ? L'L' : (Lifetime == LifetimeShortLived) ? L'S' : L'N'); _ultow_s(m_CountOfTemporariesOfVtype[TemporaryVtype]++, CharacterBuffer, _countof(CharacterBuffer), 10); NameBuffer.AppendString(CharacterBuffer); return m_Compiler->AddString(&NameBuffer); } return NULL; }
ECode FormatBase::UpTo( /* [in] */ const String& string, /* [in] */ IParsePosition* position, /* [in] */ StringBuffer& buffer, /* [in] */ Char32 stop, /* [out] */ Boolean* succeeded) { VALIDATE_NOT_NULL(succeeded); *succeeded = FALSE; VALIDATE_NOT_NULL(position); Int32 index, length; position->GetIndex(&index); AutoPtr<ArrayOf<Char32> > charArray = string.GetChars(); length = charArray->GetLength(); Boolean lastQuote = FALSE, quote = FALSE; Char32 ch; while (index < length) { ch = (*charArray)[index++]; if (ch == '\'') { if (lastQuote) { buffer.AppendChar('\''); } quote = !quote; lastQuote = TRUE; } else if (ch == stop && !quote) { position->SetIndex(index); *succeeded = TRUE; return NOERROR; } else { lastQuote = FALSE; buffer.AppendChar(ch); } } position->SetIndex(index); return NOERROR; }
ECode FormatBase::UpToWithQuotes( /* [in] */ const String& string, /* [in] */ IParsePosition* position, /* [in] */ StringBuffer& buffer, /* [in] */ Char32 stop, /* [in] */ Char32 start, /* [out] */ Boolean* succeeded) { VALIDATE_NOT_NULL(succeeded); *succeeded = FALSE; VALIDATE_NOT_NULL(position); Int32 index, length, count = 1; position->GetIndex(&index); AutoPtr<ArrayOf<Char32> > charArray = string.GetChars(); length = charArray->GetLength(); Boolean quote = FALSE; Char32 ch; while (index < length) { ch = (*charArray)[index++]; if (ch == '\'') { quote = !quote; } if (!quote) { if (ch == stop) { count--; } if (count == 0) { position->SetIndex(index); *succeeded = TRUE; return NOERROR; } if (ch == start) { count++; } } buffer.AppendChar(ch); } return E_ILLEGAL_ARGUMENT_EXCEPTION; }
ECode MessageFormat::FormatImpl( /* [in] */ ArrayOf< IInterface* >* objects, /* [in] */ IStringBuffer* inbuffer, /* [in] */ IFieldPosition* position, /* [in] */ List<AutoPtr<FieldContainer> >* fields) { VALIDATE_NOT_NULL(inbuffer); StringBuffer * buffer = (StringBuffer *)inbuffer; AutoPtr<IFieldPosition> passedField; CFieldPosition::New(0, (IFieldPosition**)&passedField); Int32 begin; String result; for (Int32 i = 0; i <= mMaxOffset; i++) { *buffer += (*mStrings)[i]; begin = buffer->GetLength(); AutoPtr<IInterface> arg; if (objects != NULL && (*mArgumentNumbers)[i] < objects->GetLength()) { arg = (*objects)[(*mArgumentNumbers)[i]]; } else { buffer->AppendChar('{'); *buffer += (*mArgumentNumbers)[i]; buffer->AppendChar('}'); HandleArgumentField(begin, buffer->GetLength(), (*mArgumentNumbers)[i], position, fields); continue; } AutoPtr<IFormat> format = (*mFormats)[i]; if (format == NULL || arg == NULL) { if (INumber::Probe(arg) != NULL) { AutoPtr<INumberFormat> nf; NumberFormat::GetInstance((INumberFormat**)&nf); format = IFormat::Probe(nf); } else if (IDate::Probe(arg) != NULL) { AutoPtr<IDateFormat> df; DateFormat::GetInstance((IDateFormat**)&df); format = IFormat::Probe(df); } else { buffer->Append(Object::ToString(arg)); HandleArgumentField(begin, buffer->GetLength(), (*mArgumentNumbers)[i], position, fields); continue; } } if (IChoiceFormat::Probe(format) != NULL) { format->Format(arg, &result); AutoPtr<IMessageFormat> mf; CMessageFormat::New(result, (IMessageFormat**)&mf); mf->SetLocale(mLocale); mf->Format(objects, buffer , passedField); HandleArgumentField(begin, buffer->GetLength(), (*mArgumentNumbers)[i], position, fields); HandleFormat(format, arg, begin, fields); } else { format->Format(arg, buffer, passedField); HandleArgumentField(begin, buffer->GetLength(), (*mArgumentNumbers)[i], position, fields); HandleFormat(format, arg, begin, fields); } } if (mMaxOffset + 1 < mStrings->GetLength()) { (*buffer) += (*mStrings)[mMaxOffset + 1]; } return NOERROR; }
void testMisc() { printf("\n==== Leave testAutoPtr ====\n"); { StringBuffer sb(20); String str; for (Int32 i = 0; i < 20; i++) { sb.AppendChar('A' + i); } str = sb.ToString(); printf(" >> Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.TrimToSize(); printf(" >> Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); } { StringBuffer sb; String str; for (Int32 i = 0; i < 20; i++) { sb.AppendChar('A' + i); } sb.InsertString(0, String("0123456789")); str = sb.ToString(); printf("\n >> InsertString Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); } { StringBuffer sb; String str; sb.AppendNULL(); str = sb.ToString(); printf("\n >> AppendNULL Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); } { StringBuffer sb(5); String str; for (Int32 i = 0; i < 5; ++i) { sb.AppendChar('1' + i); } str = sb.ToString(); printf("\n >> BeforeReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 5, String("ABCDE")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 4, String("444")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 3, String("UVWXYZ")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 7, String("1234567")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 0, String("ABC")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(0, 10, String("0123456789")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(10, 10, String("ABC")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(14, 18, String("XYZ")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); sb.Replace(12, 13, String("0")); str = sb.ToString(); printf(" >> AfterReplace Capacity %d, length %d, str: %s\n", sb.mCapacity, sb.GetLength(), str.string()); } printf("==== Leave testAutoPtr ====\n"); }
void testStringBuffer() { printf("==== Enter testStringBuffer ====\n"); printf("\n === Construct === \n"); StringBuffer sbc(String("Construct from String")); String str = sbc.ToString(); printf(" > Construct:\n%s\n", (const char*)str); printf("\n === Append === \n"); Char32 a = 'A'; StringBuffer sb; ECode ec = sb.ToString(&str); printf(">> Get string from emtpy StringBuffer %s, %08x - %08x\n", str.string(), NOERROR, ec); sb.AppendCStr(">>Start\n"); sb.AppendNULL(); sb.AppendChar('_'); sb.AppendChar(a); sb.AppendChar('_'); sb.AppendBoolean(TRUE); sb.AppendChar('_'); sb.AppendBoolean(FALSE); sb.AppendChar('_'); sb.AppendInt32(32); sb.AppendChar('_'); sb.AppendInt32(-32); sb.AppendChar('_'); sb.AppendInt64(64); sb.AppendChar('_'); sb.AppendInt64(-64); sb.AppendChar('_'); sb.AppendFloat(10.0f); sb.AppendChar('_'); sb.AppendDouble(101010.1010); sb.AppendChar('_'); sb.AppendString(String("String")); sb.AppendCStr("\n<<End"); str = sb.ToString(); printf(" > AppendTest:\n%s\n", (const char*)str); printf("\n === Index ===\n"); Int32 index = 0; String subStr("32"); sb.IndexOf(subStr, &index); printf(" > IndexOf %s is %d\n", (const char*)subStr, index); subStr = String("_NOT_"); sb.IndexOf(subStr, &index); printf(" > IndexOf %s is %d\n", (const char*)subStr, index); subStr = String("32"); sb.LastIndexOf(subStr, &index); printf(" > LastIndexOf %s is %d\n", (const char*)subStr, index); subStr = String("_NOT_"); sb.LastIndexOf(subStr, &index); printf(" > LastIndexOf %s is %d\n", (const char*)subStr, index); printf("\n === Substring ===\n"); Int32 start = 30, end = 32; sb.Substring(start, &subStr); printf(" > Substring from %d is : %s\n", start, (const char*)subStr); sb.SubstringEx(start, end, &subStr); printf(" > Substring from %d to %d is : %s\n", start, end, (const char*)subStr); printf("\n === Get ===\n"); Char32 ch = 0; sb.GetChar(start, &ch); printf(" > GetChar at %d is : %c\n", start, ch); sb.GetLength(&end); printf(" > GetLength is : %d\n", end); sb.GetByteCount(&end); printf(" > GetByteCount is : %d\n", end); sb.GetCapacity(&end); printf(" > GetCapacity is : %d\n", end); printf("\n === Set/Replace/Insert ===\n"); sb.SetChar(13, 'B'); sb.ToString(&str); printf(" > SetCharAt:\n%s\n", (const char*)str); sb.Replace(15, 15 + 4, String("Replace")); sb.ToString(&str); printf(" > Replace:\n%s\n", (const char*)str); sb.InsertString(15, String("Insert_")); sb.ToString(&str); printf(" > InsertString:\n%s\n", (const char*)str); sb.InsertString(0, String("HeadInsert_")); sb.ToString(&str); printf(" > InsertString in head:\n%s\n", (const char*)str); sb.InsertChar(19, '_'); sb.ToString(&str); printf(" > InsertChar:\n%s\n", (const char*)str); sb.InsertBoolean(19, TRUE); sb.ToString(&str); printf(" > InsertBoolean:\n%s\n", (const char*)str); sb.InsertInt32(19, 32); sb.ToString(&str); printf(" > InsertInt32:\n%s\n", (const char*)str); sb.InsertInt64(19, 64); sb.ToString(&str); printf(" > InsertInt64:\n%s\n", (const char*)str); sb.InsertFloat(19, 128.0); sb.ToString(&str); printf(" > InsertFloat:\n%s\n", (const char*)str); sb.InsertDouble(19, 10000.00001); sb.ToString(&str); printf(" > InsertDouble:\n%s\n", (const char*)str); ArrayOf<Char32>* chars = ArrayOf<Char32>::Alloc(10); for (Int32 i = 0; i < chars->GetLength(); ++i) { (*chars)[i] = 'A' + i; } sb.InsertChars(19, *chars); sb.ToString(&str); printf(" > InsertChars:\n%s\n", (const char*)str); sb.InsertCharsEx(19, *chars, 5, 5); sb.ToString(&str); printf(" > InsertCharsEx:\n%s\n", (const char*)str); printf("\n === Delete ===\n"); sb.Delete(19, 24); sb.ToString(&str); printf(" > Delete:\n%s\n", (const char*)str); sb.DeleteChar(1); sb.ToString(&str); printf(" > DeleteChar:\n%s\n", (const char*)str); printf("\n === Reverse ===\n"); sb.Reverse(); sb.ToString(&str); printf(" > Reverse:\n%s\n", (const char*)str); printf("==== Leave testStringBuffer ====\n"); }