Btk
mem.hpp
1 #if !defined(_BTKUTILS_MEM_HPP_)
2 #define _BTKUTILS_MEM_HPP_
3 #include <cstddef>
4 #include <cstdlib>
5 #include <cstring>
6 #include <string>
7 #include <string_view>
8 #include "../defs.hpp"
9 namespace Btk{
10  BTKAPI size_t U16Strlen(const char16_t *str);
11  BTKAPI int U16Strcmp(const char16_t *s1,const char16_t *s2);
12  BTKAPI int U16Strcasecmp(const char16_t *s1,const char16_t *s2);
13 
19  BTKAPI size_t Utf16To8(std::string&,std::u16string_view);
25  BTKAPI size_t Utf8To16(std::u16string&,std::string_view);
30  BTKAPI bool IsValidUtf8(std::string_view);
37  BTKAPI Sint64 ParseHex(std::string_view txt);
38  BTKAPI Sint64 ParseInt(std::string_view txt);
48  template<class T,auto Alloc = std::malloc>
49  T* Memdup(const T *ptr,size_t size){
50  T* ret = static_cast<T*>(Alloc(size));
51  if(ret == nullptr){
52  return nullptr;
53  }
54  std::memcpy(ret,ptr,size);
55  return ret;
56  }
65  template<class T,auto Alloc = std::malloc>
66  T* Memdup(const T *ptr){
67  return Memdup<T,Alloc>(ptr,sizeof(T));
68  }
69  template<class T,auto Alloc = std::malloc>
70  T* Memdup(const T &ref){
71  return Memdup<T,Alloc>(&ref,sizeof(T));
72  }
79  inline std::string Utf16To8(std::u16string_view utf16){
80  std::string utf8;
81  Utf16To8(utf8,utf16);
82  return utf8;
83  }
90  inline std::u16string Utf8To16(std::string_view utf8){
91  std::u16string utf16;
92  Utf8To16(utf16,utf8);
93  return utf16;
94  }
95 };
96 
97 #endif // _BTKUTILS_MEM_HPP_
BTKAPI Sint64 ParseHex(std::string_view txt)
Parse a hex string.
Definition: mem.cpp:197
This header include many useful containers.
Definition: async.hpp:7
BTKAPI size_t Utf16To8(std::string &, std::u16string_view)
Convert u16string to u8string.
Definition: mem.cpp:109
BTKAPI size_t Utf8To16(std::u16string &, std::string_view)
Convert u8string to u16string.
Definition: mem.cpp:114
Definition: checked.h:34
T * Memdup(const T *ptr, size_t size)
A helper template for dup memory.
Definition: mem.hpp:49
BTKAPI bool IsValidUtf8(std::string_view)
Check a string is vaid utf8.
Definition: mem.cpp:119