soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SplitString.h
1#ifndef __SPLITSTRING__H__
2#define __SPLITSTRING__H__
3
4SNSBEGIN
5
6/**
7 * @brief 拆分字符串模板函数
8 * @tparam T 字符串类型(例如,SStringA, SStringW)
9 * @tparam TC 分隔符类型(例如,char, wchar_t)
10 * @param str 要拆分的字符串
11 * @param cSep 分隔符
12 * @param strLst 存储拆分结果的数组
13 * @return 拆分后的字符串数量
14 */
15template <class T, class TC>
16size_t SplitString(const T &str, TC cSep, SArray<T> &strLst)
17{
18 int nBegin = 0;
19 int nEnd = 0;
20 while (nEnd != str.GetLength())
21 {
22 if (str[nEnd] == cSep)
23 {
24 if (nEnd > nBegin)
25 {
26 strLst.Add(str.Mid(nBegin, nEnd - nBegin));
27 }
28 nBegin = nEnd + 1;
29 }
30 nEnd++;
31 }
32 if (nEnd > nBegin)
33 {
34 strLst.Add(str.Mid(nBegin, nEnd - nBegin));
35 }
36 return strLst.GetCount();
37}
38
39/**
40 * @typedef SStringAList
41 * @brief SStringA 类型的数组
42 */
43typedef SArray<SStringA> SStringAList;
44
45/**
46 * @typedef SStringWList
47 * @brief SStringW 类型的数组
48 */
49typedef SArray<SStringW> SStringWList;
50
51// 显式模板实例化
52template size_t SplitString<SStringA, char>(const SStringA &str, char cSep, SStringAList &strLst);
53template size_t SplitString<SStringW, wchar_t>(const SStringW &str, wchar_t cSep, SStringWList &strLst);
54
55#ifdef _UNICODE
56/**
57 * @typedef SStringTList
58 * @brief 根据编译环境定义的字符串数组类型(Unicode环境为SStringWList,非Unicode环境为SStringAList)
59 */
60#define SStringTList SStringWList
61#else
62#define SStringTList SStringAList
63#endif // _UNICODE
64
65/**
66 * @brief 解析资源ID
67 * @param str 包含资源ID的字符串
68 * @param strLst 存储解析结果的数组
69 * @return 解析后的字符串数量
70 */
71inline int ParseResID(const SStringT &str, SStringTList &strLst)
72{
73 int nPos = str.FindChar(_T(':'));
74 if (nPos == -1)
75 {
76 strLst.Add(str);
77 }
78 else
79 {
80 strLst.Add(str.Left(nPos));
81 strLst.Add(str.Right(str.GetLength() - nPos - 1));
82 }
83 return (int)strLst.GetCount();
84}
85
86SNSEND
87#endif // __SPLITSTRING__H__
A class representing an ASCII string.
Definition sstringa.h:96
A class representing an ASCII string.
Definition sstringw.h:96