InternalString.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. class InternalString
  9. {
  10. Field(charcount_t) m_charLength;
  11. Field(unsigned char) m_offset;
  12. Field(const char16*) m_content;
  13. public:
  14. InternalString() : m_charLength(0), m_content(nullptr), m_offset(0) { };
  15. InternalString(const char16* content, DECLSPEC_GUARD_OVERFLOW charcount_t charLength, unsigned char offset = 0);
  16. InternalString(const char16* content, _no_write_barrier_tag, DECLSPEC_GUARD_OVERFLOW charcount_t charLength, unsigned char offset = 0);
  17. static InternalString* New(ArenaAllocator* alloc, const char16* content, DECLSPEC_GUARD_OVERFLOW charcount_t length);
  18. static InternalString* New(Recycler* recycler, const char16* content, DECLSPEC_GUARD_OVERFLOW charcount_t length);
  19. static InternalString* NewNoCopy(ArenaAllocator* alloc, const char16* content, DECLSPEC_GUARD_OVERFLOW charcount_t length);
  20. inline charcount_t GetLength() const
  21. {
  22. return m_charLength;
  23. }
  24. inline const char16* GetBuffer() const
  25. {
  26. return m_content + m_offset;
  27. }
  28. };
  29. struct InternalStringComparer
  30. {
  31. inline static bool Equals(InternalString const& str1, InternalString const& str2)
  32. {
  33. return str1.GetLength() == str2.GetLength() &&
  34. JsUtil::CharacterBuffer<char16>::StaticEquals(str1.GetBuffer(), str2.GetBuffer(), str1.GetLength());
  35. }
  36. inline static hash_t GetHashCode(InternalString const& str)
  37. {
  38. return JsUtil::CharacterBuffer<char16>::StaticGetHashCode(str.GetBuffer(), str.GetLength());
  39. }
  40. };
  41. }
  42. template<>
  43. struct DefaultComparer<Js::InternalString> : public Js::InternalStringComparer {};