Common.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma once
  2. #ifndef __STDC_LIMIT_MACROS
  3. #define __STDC_LIMIT_MACROS
  4. #endif
  5. #ifndef __STDC_CONSTANT_MACROS
  6. #define __STDC_CONSTANT_MACROS
  7. #endif
  8. // #include <string>
  9. // #include <map>
  10. // #include <set>
  11. // #include <vector>
  12. // #include <list>
  13. #include <algorithm>
  14. // #include <functional>
  15. #include <functional>
  16. #include "BFPlatform.h"
  17. inline size_t HashBytes(const uint8* ptr, size_t count) noexcept
  18. {
  19. #ifdef BF64
  20. const size_t _FNV_offset_basis = 14695981039346656037ULL;
  21. const size_t _FNV_prime = 1099511628211ULL;
  22. #else
  23. const size_t _FNV_offset_basis = 2166136261U;
  24. const size_t _FNV_prime = 16777619U;
  25. #endif
  26. size_t val = _FNV_offset_basis;
  27. for (size_t _Next = 0; _Next < count; ++_Next)
  28. {
  29. val ^= (size_t)ptr[_Next];
  30. val *= _FNV_prime;
  31. }
  32. return (val);
  33. }
  34. template <typename T>
  35. struct BeefHash : std::hash<T>
  36. {
  37. };
  38. template<class T>
  39. struct BeefHash<T*>
  40. {
  41. size_t operator()(T* val) const
  42. {
  43. return (size_t)val ^ (size_t)val >> 11;
  44. }
  45. };
  46. template <>
  47. struct BeefHash<int>
  48. {
  49. size_t operator()(int val)
  50. {
  51. return (size_t)val;
  52. }
  53. };
  54. template <>
  55. struct BeefHash<int64>
  56. {
  57. size_t operator()(int64 val)
  58. {
  59. return (size_t)val;
  60. }
  61. };
  62. struct uint128
  63. {
  64. uint64 mLo;
  65. uint64 mHigh;
  66. };
  67. struct int128
  68. {
  69. uint64 mLo;
  70. uint64 mHigh;
  71. };
  72. #define BF_MIN(x, y) (((x) < (y)) ? (x) : (y))
  73. #define BF_MAX(x, y) (((x) > (y)) ? (x) : (y))
  74. #define BF_CLAMP(val, minVal, maxVal) (((val) < (minVal)) ? (minVal) : ((val) > (maxVal)) ? (maxVal) : (val))
  75. #define BF_SWAP(a, b) { auto _a = (a); (a) = (b); (b) = (_a); }
  76. extern int gBFArgC;
  77. extern char** gBFArgV;
  78. #define NS_BF_BEGIN namespace Beefy {
  79. #define NS_BF_END }
  80. #define USING_NS_BF using namespace Beefy
  81. #define BF_ARRAY_COUNT(arr) (sizeof(arr) / sizeof((arr)[0]))
  82. #define BF_CLEAR_VALUE(val) memset(&val, 0, sizeof(val))
  83. #define BF_ALIGN(intVal, alignSize) (((intVal) + ((alignSize) - 1)) & ~((alignSize) - 1))
  84. #define BF_DISALLOW_COPY(name) name(const name& from) = delete;
  85. #define BF_ASSERT_CONCAT_(a, b) a##b
  86. #define BF_ASSERT_CONCAT(a, b) BF_ASSERT_CONCAT_(a, b)
  87. #ifdef __COUNTER__
  88. #define BF_STATIC_ASSERT(e) \
  89. ;enum { BF_ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(int)(!!(e)) }
  90. #else
  91. #define BF_STATIC_ASSERT(e) \
  92. ;enum { BF_ASSERT_CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) }
  93. #endif
  94. NS_BF_BEGIN;
  95. #ifndef max
  96. template <typename T>
  97. constexpr const T& max(const T& lhs, const T& rhs) noexcept
  98. {
  99. return (lhs < rhs) ? rhs : lhs;
  100. }
  101. template <typename T>
  102. constexpr const T& min(const T& lhs, const T& rhs) noexcept
  103. {
  104. return (lhs < rhs) ? lhs : rhs;
  105. }
  106. #endif
  107. class StringImpl;
  108. template <const int TBufSize>
  109. class StringT;
  110. class UTF16String;
  111. typedef StringT<16> String;
  112. #ifdef BF64
  113. #define V_32_64(v32, v64) v64
  114. #else
  115. #define V_32_64(v32, v64) v32
  116. #endif
  117. #define BF_PI 3.14159265359f
  118. #define BF_PI_D 3.14159265359
  119. //typedef std::vector<int> IntVector;
  120. inline float BFRound(float aVal)
  121. {
  122. if (aVal < 0)
  123. return (float) (int) (aVal - 0.5f);
  124. else
  125. return (float) (int) (aVal + 0.5f);
  126. }
  127. inline float BFClamp(float val, float min, float max)
  128. {
  129. return (val <= min) ? min : (val >= max) ? max : val;
  130. }
  131. inline int BFClamp(int val, int min, int max)
  132. {
  133. return (val <= min) ? min : (val >= max) ? max : val;
  134. }
  135. uint32 BFTickCount();
  136. void BFFatalError(const char* message, const char* file, int line);
  137. void BFFatalError(const StringImpl& message, const StringImpl& file, int line);
  138. int64 EndianSwap(int64 val);
  139. int32 EndianSwap(int32 val);
  140. int16 EndianSwap(int16 val);
  141. #ifdef BF_ENDIAN_LITTLE
  142. static inline int64 FromBigEndian(int64 val) { return Beefy::EndianSwap(val); }
  143. static inline int32 FromBigEndian(int32 val) { return Beefy::EndianSwap(val); }
  144. static inline int16 FromBigEndian(int16 val) { return Beefy::EndianSwap(val); }
  145. static inline uint64 FromBigEndian(uint64 val) { return Beefy::EndianSwap(*((int64*)&val)); }
  146. static inline uint32 FromBigEndian(uint32 val) { return Beefy::EndianSwap(*((int32*)&val)); }
  147. static inline uint16 FromBigEndian(uint16 val) { return Beefy::EndianSwap(*((int16*)&val)); }
  148. static inline int64 ToBigEndian(int64 val) { return Beefy::EndianSwap(val); }
  149. static inline int32 ToBigEndian(int32 val) { return Beefy::EndianSwap(val); }
  150. static inline int16 ToBigEndian(int16 val) { return Beefy::EndianSwap(val); }
  151. static inline uint64 ToBigEndian(uint64 val) { return Beefy::EndianSwap(*((int64*)&val)); }
  152. static inline uint32 ToBigEndian(uint32 val) { return Beefy::EndianSwap(*((int32*)&val)); }
  153. static inline uint16 ToBigEndian(uint16 val) { return Beefy::EndianSwap(*((int16*)&val)); }
  154. static inline int64 FromLittleEndian(int64 val) { return val; }
  155. static inline int32 FromLittleEndian(int32 val) { return val; }
  156. static inline int16 FromLittleEndian(int16 val) { return val; }
  157. static inline int64 FromLittleEndian(uint64 val) { return val; }
  158. static inline int32 FromLittleEndian(uint32 val) { return val; }
  159. static inline int16 FromLittleEndian(uint16 val) { return val; }
  160. #endif
  161. uint64 BFGetTickCountMicro();
  162. uint64 BFGetTickCountMicroFast();
  163. extern String vformat(const char* fmt, va_list argPtr);
  164. extern void vformat(StringImpl& str, const char* fmt, va_list argPtr);
  165. extern String StrFormat(const char* fmt ...);
  166. void ExactMinimalFloatToStr(float f, char* str);
  167. void ExactMinimalDoubleToStr(double d, char* str);
  168. String IntPtrDynAddrFormat(intptr addr);
  169. void OutputDebugStr(const StringImpl& theString);
  170. void OutputDebugStrF(const char* fmt ...);
  171. UTF16String ToWString(const StringImpl& theString);
  172. String ToString(const UTF16String& theString);
  173. String ToUpper(const StringImpl& theString);
  174. void MakeUpper(StringImpl& theString);
  175. UTF16String ToUpper(const UTF16String& theString);
  176. UTF16String ToLower(const UTF16String& theString);
  177. String ToLower(const StringImpl& theString);
  178. //UTF16String Trim(const UTF16String& theString);
  179. String Trim(const StringImpl& theString);
  180. bool StrReplace(StringImpl& str, const StringImpl& from, const StringImpl& to);
  181. bool StrStartsWith(const StringImpl& str, const StringImpl& subStr);
  182. bool StrEndsWith(const StringImpl& str, const StringImpl& subStr);
  183. String SlashString(const StringImpl& str, bool utf8decode, bool utf8encode, bool beefString = false);
  184. UTF16String UTF8Decode(const StringImpl& theString);
  185. String UTF8Encode(const UTF16String& theString);
  186. String UTF8Encode(const uint16* theString, int length);
  187. UTF16String UTF16Decode(const uint16* theString);
  188. String FileNameToURI(const StringImpl& fileName);
  189. int64 DecodeULEB32(const char*& p);
  190. void EncodeULEB32(uint64 value, StringImpl& buffer);
  191. int32 Rand();
  192. int32 GetHighestBitSet(int32 n);
  193. uint8* LoadBinaryData(const StringImpl& path, int* size);
  194. char* LoadTextData(const StringImpl& path, int* size);
  195. bool LoadTextData(const StringImpl& path, StringImpl& str);
  196. int64 GetFileTimeWrite(const StringImpl& path);
  197. String GetFileDir(const StringImpl& path);
  198. String GetFileName(const StringImpl& path);
  199. String GetFileExtension(const StringImpl& path);
  200. String GetRelativePath(const StringImpl& fullPath, const StringImpl& curDir);
  201. String GetAbsPath(const StringImpl& relPath, const StringImpl& dir);
  202. String FixPath(const StringImpl& path);
  203. String FixPathAndCase(const StringImpl& path);
  204. String EnsureEndsInSlash(const StringImpl& dir);
  205. String RemoveTrailingSlash(const StringImpl& dir);
  206. bool FileNameEquals(const StringImpl& filePathA, const StringImpl& filePathB);
  207. bool FileExists(const StringImpl& path, String* outActualName = NULL);
  208. bool DirectoryExists(const StringImpl& path, String* outActualName = NULL);
  209. bool RecursiveCreateDirectory(const StringImpl& dirName);
  210. bool RecursiveDeleteDirectory(const StringImpl& dirName);
  211. bool ParseMemorySpan(const StringImpl& str, void*& outPtr, int& outSize, StringImpl* outKey = NULL);
  212. #define CHARTAG(val) FromBIGEndian(val)
  213. int64 EndianSwap(int64 val);
  214. int32 EndianSwap(int32 val);
  215. int16 EndianSwap(int16 val);
  216. template<typename T>
  217. struct RemoveTypePointer
  218. {
  219. };
  220. template<typename T>
  221. struct RemoveTypePointer<T*>
  222. {
  223. typedef T type;
  224. };
  225. #ifndef BF_SMALL
  226. template <typename F>
  227. struct BF_Defer {
  228. F f;
  229. BF_Defer(F f) : f(f) {}
  230. ~BF_Defer() { f(); }
  231. };
  232. template <typename F>
  233. BF_Defer<F> BF_defer_func(F f) {
  234. return BF_Defer<F>(f);
  235. }
  236. #define DEFER_1(x, y) x##y
  237. #define DEFER_2(x, y) DEFER_1(x, y)
  238. #define DEFER_3(x) DEFER_2(x, __COUNTER__)
  239. #define defer(code) auto DEFER_3(_defer_) = BF_defer_func([&](){code;})
  240. #endif //BF_SMALL
  241. NS_BF_END
  242. #include "util/Array.h"
  243. #include "util/String.h"