CString.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #ifndef __DAHUA_INFRA_CSTRING_H__
  2. #define __DAHUA_INFRA_CSTRING_H__
  3. #include <stddef.h> /// for size_t
  4. #include <Infra/Defs.h>
  5. namespace Dahua{
  6. namespace Infra{
  7. /// 字符串操作类
  8. class INFRA_API CString
  9. {
  10. public:
  11. /// 字符串尾索引
  12. static const size_t npos;
  13. /// 默认构造函数
  14. CString();
  15. /// 构造函数
  16. /// \param str 拷贝构造提供的CString
  17. CString( CString const& str );
  18. /// 构造函数
  19. /// \param str 被构造的原始字符串
  20. /// \param pos 第一个构造字符在原字符串中的偏移位置
  21. /// \param len 构造字符串长度
  22. CString( CString const& str, size_t pos, size_t len );
  23. /// 构造函数
  24. /// \param s 构造字符串
  25. CString( const char* s );
  26. /// 构造函数
  27. /// \param s 被构造字符串
  28. /// \param n 构造字符串长度
  29. CString( const char* s, size_t n );
  30. /// 构造函数
  31. /// \param c 构造元字符
  32. /// \param n 构造元字符的数量
  33. CString( char c, size_t n );
  34. /// 析构函数
  35. ~CString();
  36. /// 赋值构造函数
  37. /// \param str 赋值字符串
  38. CString& operator=( CString const& str );
  39. /// 赋值构造函数
  40. /// \param str 赋值字符串
  41. CString& operator=( const char* str );
  42. /// 赋值构造函数
  43. /// \param c 使用字符构造长度为1的字符串
  44. CString& operator=( char c );
  45. /// 获取字符串长度
  46. size_t size() const;
  47. /// 获取字符串长度
  48. size_t length() const;
  49. /// 重置字符串长度
  50. /// \param n 重置后字符串长度
  51. void resize( size_t n );
  52. /// 使用指定字符重置字符串长度
  53. /// \param c 指定字符串
  54. /// \param n 重置后字符串长度
  55. void resize( char c, size_t n );
  56. /// 返回字符串占用内存大小
  57. size_t capacity() const;
  58. /// 设置字符串的存储长度
  59. /// \param n 字符存储长度
  60. void reserve( size_t n );
  61. /// 清除字符串数据
  62. void clear();
  63. /// 判断字符串是为空
  64. bool empty() const;
  65. /// 获取字符串中指定索引字符
  66. /// \param pos 索引值
  67. char const& operator[]( size_t pos ) const;
  68. /// 获取字符串中指定索引字符
  69. /// \param pos 索引值
  70. char& operator[]( size_t pos );
  71. /// 获取字符串中指定索引字符
  72. /// \param n 索引值
  73. char const& at( size_t n ) const;
  74. /// 获取字符串中指定索引字符
  75. /// \param n 索引值
  76. char& at( size_t n );
  77. /// 重载自加操作符
  78. /// \param str 被追加字符串
  79. CString& operator+=( CString const& str );
  80. /// 重载自加操作符
  81. /// \param str 被追加字符串
  82. CString& operator+=( const char* str );
  83. /// 重载自加操作符
  84. /// \param c 被追加字符
  85. CString& operator+=( const char c );
  86. /// 追加字符串
  87. /// \param str 追加的字符串
  88. CString& append( CString const& str );
  89. /// 追加指定长度字符串
  90. /// \param str 追加的字符串
  91. /// \param pos 追加字符串的偏移量
  92. /// \param n 追加字符长度
  93. CString& append( CString const& str, size_t pos, size_t n );
  94. /// 追加指定长度的字符串
  95. /// \param s 追加字符串地址
  96. /// \param n 追加字符串长度
  97. CString& append( const char* s, size_t n );
  98. /// 追加指定字符串
  99. /// \param s 追加字符串地址
  100. CString& append( const char* s );
  101. /// 增加自定字符
  102. /// \param c 追加字符
  103. void push_back( const char c );
  104. /// 将其他字符串分配给自身
  105. /// \param str 分配字符串
  106. CString& assign( CString const& str );
  107. /// \指定长度字符串分配
  108. /// \param str 原始待分配字符串
  109. /// \param pos 分配字符串偏移量
  110. /// \param n 分配字符串长度
  111. CString& assign( CString const& str, size_t pos, size_t n );
  112. /// 使用字符串地址分配字符串
  113. /// \param s 字符串地址
  114. /// \param n 字符长度
  115. CString& assign( const char* s, size_t n );
  116. /// 从指定地址分配字符串
  117. /// \param s 分配字符串地址
  118. CString& assign( const char* s );
  119. /// 在指定位置插入指定字符串
  120. /// \param pos 插入位置偏移量
  121. /// \param str 插入字符串
  122. CString& insert( size_t pos, CString const& str );
  123. /// 在指定位置插入指定长度的字符串
  124. /// \param pos 插入位置偏移量
  125. /// \param str 原插入字符串
  126. /// \param pos 插入字符串基于原插入字符串的偏移量
  127. /// \param n 插入字符串的长度
  128. CString& insert( size_t pos1, CString const& str,
  129. size_t pos2, size_t n );
  130. /// 在指定位置插入指定字符串
  131. /// \param pos 插入位置偏移量
  132. /// \param s 插入字符串地址
  133. /// \param n 插入字符串长度
  134. CString& insert( size_t pos, const char* s, size_t n );
  135. /// 在指定位置插入指定字符串
  136. /// \param pos 插入位置偏移量
  137. /// \param s 插入字符串地址
  138. CString& insert( size_t pos, const char* s );
  139. /// 在指定位置插入n个指定元字符
  140. /// \param pos 插入位置偏移量
  141. /// \param c 元字符
  142. /// \param n 元字符数量
  143. CString& insert( size_t pos, char c, size_t n );
  144. /// 在指定位置插入元字符
  145. /// \param pos 插入位置偏移量
  146. /// \param c 元字符
  147. CString& insert( size_t pos, char c );
  148. /// 在指定位置删除n个字符
  149. /// \param pos 删除起始位置偏移量
  150. /// \param n 删除字符数量
  151. CString& erase( size_t pos, size_t n );
  152. /// 将指定位置的n个字符替换为另一字符串
  153. /// \param pos 替换位置的偏移量
  154. /// \param n 替换字符数量
  155. /// \param str 替换字符串
  156. CString& replace( size_t pos, size_t n, CString const& str );
  157. /// 将指定位置的n个字符替换为另一字符串
  158. /// \param pos1 替换位置的偏移量
  159. /// \param n1 替换字符数量
  160. /// \param str 用于替换的原始字符串
  161. /// \param pos2 替换字符串在str中的偏移量
  162. CString& replace( size_t pos1, size_t n,
  163. CString const& str, size_t pos2 );
  164. /// 将指定位置的n个字符替换为另一字符串
  165. /// \param pos1 替换位置的偏移量
  166. /// \param n1 替换字符数量
  167. /// \param s 替换字符串地址
  168. CString& replace( size_t pos1, size_t n1, const char* s );
  169. /// 从指定位置拷贝n个字符串到指定地址
  170. /// \param s[ out ] 接收字符串地址
  171. /// \param n[ int ] 拷贝字符串长度
  172. /// \param pos[ int ] 拷贝起始位置偏移量
  173. size_t copy( char* s, size_t n, size_t pos = 0 ) const;
  174. /// 交换两个字符串
  175. /// \param other 被交换字符串
  176. void swap( CString& other );
  177. /// 获取字符串的起始指针
  178. const char* c_str() const;
  179. /// 获取字符串的起始指针
  180. const char* data() const;
  181. /// 查找指定字符串
  182. /// \param str 查询字符串
  183. /// \param pos 查询起始位置的偏移量
  184. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  185. size_t find( CString const& str, size_t pos = 0 ) const;
  186. /// 查找指定字符串
  187. /// \param s 查询字符串地址
  188. /// \param pos 查询起始位置的偏移量
  189. /// \param n 指定查询串的长度
  190. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  191. size_t find( const char* s, size_t pos, size_t n ) const;
  192. /// 查找指定字符串
  193. /// \param s 查询字符串地址
  194. /// \param pos 查询起始位置的偏移量
  195. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  196. size_t find( const char* s, size_t pos = 0 ) const;
  197. /// 查询指定字符
  198. /// \param s 查询字符
  199. /// \param pos 查询起始位置的偏移量
  200. /// \查询成功,返回字符串所在位置,查询失败返回npos
  201. size_t find( char c, size_t pos = 0 ) const;
  202. /// 反向查询指定字符串
  203. /// \param str 查询字符串
  204. /// \param pos 查询起始位置的偏移量
  205. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  206. size_t rfind( CString const& str, size_t pos = npos ) const;
  207. /// 反向查找指定字符串
  208. /// \param s 查询字符串地址
  209. /// \param pos 查询起始位置的偏移量
  210. /// \param n 指定查询串的长度
  211. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  212. size_t rfind( const char* s, size_t pos, size_t n ) const;
  213. /// 反向查找指定字符串
  214. /// \param s 查询字符串地址
  215. /// \param pos 查询起始位置的偏移量
  216. /// \return 查询成功,返回字符串所在位置,查询失败返回npos
  217. size_t rfind( const char* s, size_t pos = npos ) const;
  218. /// 反向查询指定字符
  219. /// \param s 查询字符
  220. /// \param pos 查询起始位置的偏移量
  221. /// \查询成功,返回字符串所在位置,查询失败返回npos
  222. size_t rfind( char c, size_t pos = npos ) const;
  223. /// 查询第一个匹配指定字符串的位置
  224. /// \param str 匹配字符串
  225. /// \param pos 开始匹配偏移量
  226. size_t find_first_of( CString const& str, size_t pos = 0 ) const;
  227. /// 查询第一个匹配指定字符串的位置
  228. /// \param s 匹配字符串地址
  229. /// \param pos 开始匹配偏移量
  230. /// \param n 指定匹配字符串长度
  231. size_t find_first_of( const char* s, size_t pos, size_t n ) const;
  232. /// 查询第一个匹配指定字符串的位置
  233. /// \param s 匹配字符串地址
  234. /// \param pos 开始匹配偏移量
  235. size_t find_first_of( const char* s, size_t pos = 0 ) const;
  236. /// 查询第一个匹配指定字符的位置
  237. /// \param c 匹配字符
  238. /// \param pos 开始匹配偏移量
  239. size_t find_first_of( char c, size_t pos = 0 ) const;
  240. /// 从后向前查询,查找第一个匹配到指定字符串的位置
  241. /// \param str 匹配字符串
  242. /// \param pos 开始匹配偏移量
  243. size_t find_last_of( CString const& str, size_t pos = 0 ) const;
  244. /// 从后向前查询,查找第一个匹配到指定字符串的位置
  245. /// \param s 匹配字符串地址
  246. /// \param pos 开始匹配偏移量
  247. /// \param n 指定匹配字符串长度
  248. size_t find_last_of( const char* s, size_t pos, size_t n ) const;
  249. /// 从后向前查询,查找第一个匹配到指定字符串的位置
  250. /// \param s 匹配字符串地址
  251. /// \param pos 开始匹配偏移量
  252. size_t find_last_of( const char* s, size_t pos = 0 ) const;
  253. /// 从后向前查询,查询第一个匹配指定字符的位置
  254. /// \param c 匹配字符
  255. /// \param pos 开始匹配偏移量
  256. size_t find_last_of( char c, size_t pos = 0 ) const;
  257. /// 在原字符串上截取子字符串
  258. /// \param pos 子字符串其实位置偏移量
  259. /// \param n 子字符串长度
  260. CString substr( size_t pos = 0, size_t n = npos ) const;
  261. /// 比较两个字符串是否相等
  262. /// \param str 比较字符串
  263. int compare( CString const& str ) const;
  264. /// 比较两个字符串是否相等
  265. /// \param pos1 被比较字符串的起始偏移量
  266. /// \param n1 指定被比较字符串长度
  267. /// \param str 比较字符串
  268. int compare( size_t pos1, size_t n1, CString const& str ) const;
  269. /// 比较两个字符串是否相等
  270. /// \param pos1 被比较字符串的起始偏移量
  271. /// \param n1 指定被比较字符串长度
  272. /// \param s 比较字符串地址
  273. int compare( size_t pos1, size_t n1, const char* s ) const;
  274. /// 比较两个字符串是否相等
  275. /// \param pos1 被比较字符串的起始偏移量
  276. /// \param n1 指定被比较字符串长度
  277. /// \param s 比较字符串地址
  278. /// \param n2 比较长度
  279. int compare( size_t pos1, size_t n1, const char* s, size_t n2 ) const;
  280. /// 比较两个字符串是否相等
  281. /// \param pos1 被比较字符串的起始偏移量
  282. /// \param n1 指定被比较字符串长度
  283. /// \param str 比较字符串
  284. /// \param pos2 比较字符串的起始偏移量
  285. /// \param n2 指定比较字符串长度
  286. int compare( size_t pos1, size_t n1, CString const& str,
  287. size_t pos2 ) const;
  288. /// 比较两个字符串是否相等
  289. /// \param s 比较字符串地址
  290. int compare( const char* s ) const;
  291. /// 重载 + 操作符
  292. /// \param b 加字符串
  293. CString operator + ( CString const& other ) const;
  294. /// 重载 + 操作符
  295. /// \param b 加字符串
  296. CString operator + ( const char* other ) const;
  297. /// 重载 == 操作符
  298. /// \param b 比较字符串
  299. bool operator == ( CString const& other ) const;
  300. /// 重载 == 操作符
  301. /// \param b 比较字符串地址
  302. bool operator == ( const char* other ) const;
  303. /// 重载 != 操作符
  304. /// \param b 比较字符串地址
  305. bool operator != ( CString const& other ) const;
  306. /// 重载 != 操作符
  307. /// \param b 比较字符串地址
  308. bool operator != ( const char* other ) const;
  309. /// 重载 < 操作符
  310. /// \param b 比较字符串地址
  311. bool operator < ( CString const& other ) const;
  312. /// 重载 < 操作符
  313. /// \param b 比较字符串地址
  314. bool operator < ( const char* other ) const;
  315. /// 重载 <= 操作符
  316. /// \param b 比较字符串地址
  317. bool operator <= ( CString const& other ) const;
  318. /// 重载 <= 操作符
  319. /// \param b 比较字符串地址
  320. bool operator <= ( const char* other ) const;
  321. /// 重载 > 操作符
  322. /// \param b 比较字符串地址
  323. bool operator > ( CString const& other ) const;
  324. /// 重载 > 操作符
  325. /// \param b 比较字符串地址
  326. bool operator > ( const char* other ) const;
  327. /// 重载 >= 操作符
  328. /// \param b 比较字符串地址
  329. bool operator >= ( CString const& other ) const;
  330. /// 重载 >= 操作符
  331. /// \param b 比较字符串地址
  332. bool operator >= ( const char* other ) const;
  333. private:
  334. struct Internal;
  335. Internal* m_internal;
  336. };
  337. } /// namespace Infra
  338. } /// namespace Dahua
  339. #endif /// __DAHUA_INFRA_CSTRING_H__