Commit a29c1dfa authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by facebook-github-bot-4

Prefer constexpr to preprocessor macros in FBString.h

Summary: [Folly] Prefer `constexpr` to preprocessor macros in `FBString.h`.

Reviewed By: @JoelMarcey

Differential Revision: D2441709
parent 40673e01
...@@ -282,17 +282,20 @@ private: ...@@ -282,17 +282,20 @@ private:
* to extract capacity/category. * to extract capacity/category.
*/ */
template <class Char> class fbstring_core { template <class Char> class fbstring_core {
protected:
static constexpr bool kIsLittleEndian =
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
static constexpr bool kIsBigEndian =
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
static_assert(
kIsLittleEndian || kIsBigEndian, "unable to identify endianness");
public: public:
fbstring_core() noexcept { fbstring_core() noexcept {
// Only initialize the tag, will set the MSBs (i.e. the small // Only initialize the tag, will set the MSBs (i.e. the small
// string size) to zero too // string size) to zero too
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ml_.capacity_ = kIsLittleEndian
ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char))); ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)))
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ : ml_.capacity_ = maxSmallSize << 2;
ml_.capacity_ = maxSmallSize << 2;
#else
#error Unable to identify target endianness
#endif
// or: setSmallSize(0); // or: setSmallSize(0);
writeTerminator(); writeTerminator();
assert(category() == Category::isSmall && size() == 0); assert(category() == Category::isSmall && size() == 0);
...@@ -792,15 +795,12 @@ private: ...@@ -792,15 +795,12 @@ private:
enum class Category : category_type { enum class Category : category_type {
isSmall = 0, isSmall = 0,
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ isMedium = kIsLittleEndian
isMedium = sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000, ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000
isLarge = sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000, : 0x2,
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ isLarge = kIsLittleEndian
isMedium = 0x2, ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000
isLarge = 0x1, : 0x1,
#else
#error Unable to identify target endianness
#endif
}; };
Category category() const { Category category() const {
...@@ -814,23 +814,15 @@ private: ...@@ -814,23 +814,15 @@ private:
size_t capacity_; size_t capacity_;
size_t capacity() const { size_t capacity() const {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ return kIsLittleEndian
return capacity_ & capacityExtractMask; ? capacity_ & capacityExtractMask
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ : capacity_ >> 2;
return capacity_ >> 2;
#else
#error Unable to identify target endianness
#endif
} }
void setCapacity(size_t cap, Category cat) { void setCapacity(size_t cap, Category cat) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ capacity_ = kIsLittleEndian
capacity_ = cap | static_cast<category_type>(cat); ? cap | static_cast<category_type>(cat)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ : (cap << 2) | static_cast<category_type>(cat);
capacity_ = (cap << 2) | static_cast<category_type>(cat);
#else
#error Unable to identify target endianness
#endif
} }
}; };
...@@ -844,34 +836,22 @@ private: ...@@ -844,34 +836,22 @@ private:
maxSmallSize = lastChar / sizeof(Char), maxSmallSize = lastChar / sizeof(Char),
maxMediumSize = 254 / sizeof(Char), // coincides with the small maxMediumSize = 254 / sizeof(Char), // coincides with the small
// bin size in dlmalloc // bin size in dlmalloc
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ categoryExtractMask = kIsLittleEndian
categoryExtractMask = sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000, ? sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000
capacityExtractMask = ~categoryExtractMask, : 0x3,
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ capacityExtractMask = kIsLittleEndian
categoryExtractMask = 0x3, ? ~categoryExtractMask
#else : 0x0 /*unused*/,
#error Unable to identify target endianness
#endif
}; };
static_assert(!(sizeof(MediumLarge) % sizeof(Char)), static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
"Corrupt memory layout for fbstring."); "Corrupt memory layout for fbstring.");
size_t smallSize() const { size_t smallSize() const {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ assert(category() == Category::isSmall);
assert(category() == Category::isSmall && auto shift = kIsLittleEndian ? 0 : 2;
static_cast<size_t>(small_[maxSmallSize]) auto smallShifted = static_cast<size_t>(small_[maxSmallSize]) >> shift;
<= static_cast<size_t>(maxSmallSize)); assert(static_cast<size_t>(maxSmallSize) >= smallShifted);
return static_cast<size_t>(maxSmallSize) return static_cast<size_t>(maxSmallSize) - smallShifted;
- static_cast<size_t>(small_[maxSmallSize]);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
assert(category() == Category::isSmall &&
(static_cast<size_t>(small_[maxSmallSize]) >> 2)
<= static_cast<size_t>(maxSmallSize));
return static_cast<size_t>(maxSmallSize)
- (static_cast<size_t>(small_[maxSmallSize]) >> 2);
#else
#error Unable to identify target endianness
#endif
} }
void setSmallSize(size_t s) { void setSmallSize(size_t s) {
...@@ -879,13 +859,9 @@ private: ...@@ -879,13 +859,9 @@ private:
// so don't assume anything about the previous value of // so don't assume anything about the previous value of
// small_[maxSmallSize]. // small_[maxSmallSize].
assert(s <= maxSmallSize); assert(s <= maxSmallSize);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ small_[maxSmallSize] = kIsLittleEndian
small_[maxSmallSize] = maxSmallSize - s; ? maxSmallSize - s
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ : (maxSmallSize - s) << 2;
small_[maxSmallSize] = (maxSmallSize - s) << 2;
#else
#error Unable to identify target endianness
#endif
writeTerminator(); writeTerminator();
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment