PropertyName.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef PropertyName_h
  26. #define PropertyName_h
  27. #include "Identifier.h"
  28. #include "PrivateName.h"
  29. namespace JSC {
  30. template <typename CharType>
  31. ALWAYS_INLINE uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length)
  32. {
  33. // An empty string is not a number.
  34. if (!length)
  35. return UINT_MAX;
  36. // Get the first character, turning it into a digit.
  37. uint32_t value = characters[0] - '0';
  38. if (value > 9)
  39. return UINT_MAX;
  40. // Check for leading zeros. If the first characher is 0, then the
  41. // length of the string must be one - e.g. "042" is not equal to "42".
  42. if (!value && length > 1)
  43. return UINT_MAX;
  44. while (--length) {
  45. // Multiply value by 10, checking for overflow out of 32 bits.
  46. if (value > 0xFFFFFFFFU / 10)
  47. return UINT_MAX;
  48. value *= 10;
  49. // Get the next character, turning it into a digit.
  50. uint32_t newValue = *(++characters) - '0';
  51. if (newValue > 9)
  52. return UINT_MAX;
  53. // Add in the old value, checking for overflow out of 32 bits.
  54. newValue += value;
  55. if (newValue < value)
  56. return UINT_MAX;
  57. value = newValue;
  58. }
  59. return value;
  60. }
  61. ALWAYS_INLINE uint32_t toUInt32FromStringImpl(StringImpl* impl)
  62. {
  63. if (impl->is8Bit())
  64. return toUInt32FromCharacters(impl->characters8(), impl->length());
  65. return toUInt32FromCharacters(impl->characters16(), impl->length());
  66. }
  67. class PropertyName {
  68. public:
  69. PropertyName(const Identifier& propertyName)
  70. : m_impl(propertyName.impl())
  71. {
  72. ASSERT(!m_impl || m_impl->isIdentifier());
  73. }
  74. PropertyName(const PrivateName& propertyName)
  75. : m_impl(propertyName.uid())
  76. {
  77. ASSERT(m_impl && m_impl->isEmptyUnique());
  78. }
  79. StringImpl* uid() const
  80. {
  81. ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique()));
  82. return m_impl;
  83. }
  84. StringImpl* publicName() const
  85. {
  86. ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique()));
  87. return m_impl->isIdentifier() ? m_impl : 0;
  88. }
  89. static const uint32_t NotAnIndex = UINT_MAX;
  90. uint32_t asIndex()
  91. {
  92. ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique()));
  93. return m_impl ? toUInt32FromStringImpl(m_impl) : NotAnIndex;
  94. }
  95. private:
  96. StringImpl* m_impl;
  97. };
  98. inline bool operator==(PropertyName a, const Identifier& b)
  99. {
  100. return a.uid() == b.impl();
  101. }
  102. inline bool operator==(const Identifier& a, PropertyName b)
  103. {
  104. return a.impl() == b.uid();
  105. }
  106. inline bool operator==(PropertyName a, PropertyName b)
  107. {
  108. return a.uid() == b.uid();
  109. }
  110. inline bool operator!=(PropertyName a, const Identifier& b)
  111. {
  112. return a.uid() != b.impl();
  113. }
  114. inline bool operator!=(const Identifier& a, PropertyName b)
  115. {
  116. return a.impl() != b.uid();
  117. }
  118. inline bool operator!=(PropertyName a, PropertyName b)
  119. {
  120. return a.uid() != b.uid();
  121. }
  122. }
  123. #endif