BufferStringBuilder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. // Used when the final string has a known final length
  9. class BufferStringBuilder
  10. {
  11. public:
  12. BufferStringBuilder(charcount_t charLength, ScriptContext* scriptContext)
  13. : m_string( WritableString::New(charLength, scriptContext) )
  14. {
  15. }
  16. JavascriptString* ToString();
  17. void DbgAssertNotFrozen() const
  18. {
  19. AssertMsg(this->m_string != nullptr, "Do not call BufferStringBuilder methods after ToString() has been called.");
  20. }
  21. void SetContent(
  22. const wchar_t* prefix, charcount_t cchPrefix,
  23. const wchar_t* content, charcount_t cchContent,
  24. const wchar_t* suffix, charcount_t cchSuffix)
  25. {
  26. DbgAssertNotFrozen();
  27. this->m_string->SetContent(prefix, cchPrefix, content, cchContent, suffix, cchSuffix);
  28. }
  29. // Caution: Do not retain the writable buffer after ToString has been called
  30. wchar_t* DangerousGetWritableBuffer()
  31. {
  32. DbgAssertNotFrozen();
  33. return this->m_string->GetWritableBuffer();
  34. }
  35. class WritableString sealed : public JavascriptString
  36. {
  37. public:
  38. static WritableString* New(charcount_t length, ScriptContext* scriptContext);
  39. wchar_t* GetWritableBuffer() const
  40. {
  41. return const_cast< wchar_t* >( this->UnsafeGetBuffer() );
  42. }
  43. void SetContent(const wchar_t* content, charcount_t offset, charcount_t length);
  44. void SetContent(const wchar_t* prefix, charcount_t cchPrefix,
  45. const wchar_t* content, charcount_t cchContent,
  46. const wchar_t* suffix, charcount_t cchSuffix);
  47. protected:
  48. DEFINE_VTABLE_CTOR(BufferStringBuilder::WritableString, JavascriptString);
  49. DECLARE_CONCRETE_STRING_CLASS;
  50. private:
  51. WritableString(StaticType * type, charcount_t length, const wchar_t* szValue)
  52. : JavascriptString(type, length, szValue)
  53. {
  54. }
  55. static wchar_t* SafeCopyAndAdvancePtr(__out_ecount(cchDst) wchar_t* dst, charcount_t& cchDst, __in_ecount(cch) const wchar_t* ptr, charcount_t cch);
  56. };
  57. private:
  58. WritableString* m_string;
  59. #if DBG
  60. static const wchar_t k_dbgFill = L'\xCDCD';
  61. #endif
  62. };
  63. // Needed by diagnostics vtable access
  64. typedef BufferStringBuilder::WritableString BufferStringBuilderWritableString;
  65. } // namespace Js