Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.0 KiB

  1. //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains some functions that are useful when dealing with strings.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_STRINGEXTRAS_H
  14. #define LLVM_ADT_STRINGEXTRAS_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/DataTypes.h"
  17. namespace llvm {
  18. template<typename T> class SmallVectorImpl;
  19. /// hexdigit - Return the hexadecimal character for the
  20. /// given number \p X (which should be less than 16).
  21. static inline char hexdigit(unsigned X, bool LowerCase = false) {
  22. const char HexChar = LowerCase ? 'a' : 'A';
  23. return X < 10 ? '0' + X : HexChar + X - 10;
  24. }
  25. /// Interpret the given character \p C as a hexadecimal digit and return its
  26. /// value.
  27. ///
  28. /// If \p C is not a valid hex digit, -1U is returned.
  29. static inline unsigned hexDigitValue(char C) {
  30. if (C >= '0' && C <= '9') return C-'0';
  31. if (C >= 'a' && C <= 'f') return C-'a'+10U;
  32. if (C >= 'A' && C <= 'F') return C-'A'+10U;
  33. return -1U;
  34. }
  35. /// utohex_buffer - Emit the specified number into the buffer specified by
  36. /// BufferEnd, returning a pointer to the start of the string. This can be used
  37. /// like this: (note that the buffer must be large enough to handle any number):
  38. /// char Buffer[40];
  39. /// printf("0x%s", utohex_buffer(X, Buffer+40));
  40. ///
  41. /// This should only be used with unsigned types.
  42. ///
  43. template<typename IntTy>
  44. static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
  45. char *BufPtr = BufferEnd;
  46. *--BufPtr = 0; // Null terminate buffer.
  47. if (X == 0) {
  48. *--BufPtr = '0'; // Handle special case.
  49. return BufPtr;
  50. }
  51. while (X) {
  52. unsigned char Mod = static_cast<unsigned char>(X) & 15;
  53. *--BufPtr = hexdigit(Mod);
  54. X >>= 4;
  55. }
  56. return BufPtr;
  57. }
  58. static inline std::string utohexstr(uint64_t X) {
  59. char Buffer[17];
  60. return utohex_buffer(X, Buffer+17);
  61. }
  62. static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
  63. char Buffer[11];
  64. char *BufPtr = Buffer+11;
  65. if (X == 0) *--BufPtr = '0'; // Handle special case...
  66. while (X) {
  67. *--BufPtr = '0' + char(X % 10);
  68. X /= 10;
  69. }
  70. if (isNeg) *--BufPtr = '-'; // Add negative sign...
  71. return std::string(BufPtr, Buffer+11);
  72. }
  73. static __declspec(noinline) inline std::string utostr(uint64_t X, bool isNeg = false) {
  74. char Buffer[21];
  75. char *BufPtr = Buffer+21;
  76. if (X == 0) *--BufPtr = '0'; // Handle special case...
  77. while (X) {
  78. *--BufPtr = '0' + char(X % 10);
  79. X /= 10;
  80. }
  81. if (isNeg) *--BufPtr = '-'; // Add negative sign...
  82. return std::string(BufPtr, Buffer+21);
  83. }
  84. static inline std::string itostr(int64_t X) {
  85. if (X < 0)
  86. return utostr(static_cast<uint64_t>(-X), true);
  87. else
  88. return utostr(static_cast<uint64_t>(X));
  89. }
  90. /// StrInStrNoCase - Portable version of strcasestr. Locates the first
  91. /// occurrence of string 's1' in string 's2', ignoring case. Returns
  92. /// the offset of s2 in s1 or npos if s2 cannot be found.
  93. StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
  94. /// getToken - This function extracts one token from source, ignoring any
  95. /// leading characters that appear in the Delimiters string, and ending the
  96. /// token at any of the characters that appear in the Delimiters string. If
  97. /// there are no tokens in the source string, an empty string is returned.
  98. /// The function returns a pair containing the extracted token and the
  99. /// remaining tail string.
  100. std::pair<StringRef, StringRef> getToken(StringRef Source,
  101. StringRef Delimiters = " \t\n\v\f\r");
  102. /// SplitString - Split up the specified string according to the specified
  103. /// delimiters, appending the result fragments to the output list.
  104. void SplitString(StringRef Source,
  105. SmallVectorImpl<StringRef> &OutFragments,
  106. StringRef Delimiters = " \t\n\v\f\r");
  107. /// HashString - Hash function for strings.
  108. ///
  109. /// This is the Bernstein hash function.
  110. //
  111. // FIXME: Investigate whether a modified bernstein hash function performs
  112. // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
  113. // X*33+c -> X*33^c
  114. static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
  115. for (unsigned i = 0, e = Str.size(); i != e; ++i)
  116. Result = Result * 33 + (unsigned char)Str[i];
  117. return Result;
  118. }
  119. /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
  120. static inline StringRef getOrdinalSuffix(unsigned Val) {
  121. // It is critically important that we do this perfectly for
  122. // user-written sequences with over 100 elements.
  123. switch (Val % 100) {
  124. case 11:
  125. case 12:
  126. case 13:
  127. return "th";
  128. default:
  129. switch (Val % 10) {
  130. case 1: return "st";
  131. case 2: return "nd";
  132. case 3: return "rd";
  133. default: return "th";
  134. }
  135. }
  136. }
  137. } // End llvm namespace
  138. #endif