This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsIRC_StringTokenizer.cpp
111 lines (83 loc) · 2.55 KB
/
zsIRC_StringTokenizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// zsIRC_StringTokenizer.cpp: implementation of the zsIRC_StringTokenizer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "zsIRC.h"
#include "zsIRC_StringTokenizer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
zsIRC_StringTokenizer::zsIRC_StringTokenizer(CString s)
{
m_sData = s;
}
zsIRC_StringTokenizer::~zsIRC_StringTokenizer()
{
}
int zsIRC_StringTokenizer::GetOccurrences(TCHAR cChar) {
CString sLocal = Collapse(cChar);
int n=0;
for (int i=0; i<sLocal.GetLength(); i++)
if (sLocal.GetAt(i)==cChar) n++;
return n;
}
CString zsIRC_StringTokenizer::Collapse(TCHAR cChar) {
CString original = m_sData;
CString edited = m_sData;
CString oldchunk = cChar;
oldchunk += cChar;
CString newchunk = cChar;
while (1) {
CString before = edited;
edited.Replace(oldchunk,newchunk);
if (before==edited) break;
}
return edited;
}
CString zsIRC_StringTokenizer::GetSlice(TCHAR cChar,int nPiece) {
CString s="";
CString sLocal = Collapse(cChar);
int nOcc = GetOccurrences(cChar);
if (nOcc==0) {
CString s2 = m_sData;
return s2;
}
if (nPiece<0) return s;
if (nPiece>nOcc) return s;
if (nPiece==0) return m_sData.Left(sLocal.Find(cChar));
if (nPiece==nOcc) return m_sData.Right(sLocal.GetLength() - sLocal.ReverseFind(cChar) - 1);
int nStart = 0;
for (int i=0; i<nPiece; i++) nStart = sLocal.Find(cChar,nStart) + 1;
return sLocal.Mid(nStart, sLocal.Find(cChar,nStart+1) - nStart);
}
CString zsIRC_StringTokenizer::GetRightSlice(TCHAR cChar) {
CString s="";
CString sLocal = Collapse(cChar);
int nPos = sLocal.Find(cChar);
if (nPos == -1) return s;
else return sLocal.Mid(nPos+1);
}
CString zsIRC_StringTokenizer::GetRightSliceWithoutCollapse(TCHAR cChar) {
CString s="";
CString sLocal = m_sData;
int nPos = sLocal.Find(cChar);
if (nPos == -1) return s;
else return sLocal.Mid(nPos+1);
}
CString zsIRC_StringTokenizer::GetRightSlice(TCHAR cChar, int num) {
CString s="";
CString sLocal = Collapse(cChar);
if (num>=GetOccurrences(cChar))
return s;
int nPos = 0;
for (int i=0; i<num+1; i++) {
if (nPos == -1) return s;
nPos = sLocal.Find(cChar,nPos)+1;
}
return sLocal.Mid(nPos);
}