本文整理汇总了C++中SPString::length方法的典型用法代码示例。如果您正苦于以下问题:C++ SPString::length方法的具体用法?C++ SPString::length怎么用?C++ SPString::length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SPString
的用法示例。
在下文中一共展示了SPString::length方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: length
int
SPString::rindex(const SPString & s, int offset)
{
if (offset == -1)
offset = length() - s.length();
else
offset = offset - s.length() + 1;
if (offset > length() - s.length())
offset = length() - s.length();
for (int i = offset; i >= 0; i--) {
if (strncmp(&pstr[i], s, s.length()) == 0)
return i;
}
return -1;
}
示例2:
int
SPString::index(const SPString & s, int offset)
{
if (offset < 0)
offset = 0;
for (int i = offset; i < length(); i++) {
if (strncmp(&pstr[i], s, s.length()) == 0)
return i;
}
return -1;
}
示例3: if
//
// I know! This is not fast, but it works!!
//
int
SPString::tr(const char *sl, const char *rl, const char *opts)
{
if (length() == 0 || strlen(sl) == 0)
return 0;
int cflg = strchr(opts, 'c') != NULL; // thanks Michael
int dflg = strchr(opts, 'd') != NULL;
int sflg = strchr(opts, 's') != NULL;
int cnt = 0,
flen = 0;
unsigned int i;
SPString t;
unsigned char lstc = '\0',
fr[256];
// build search array, which is a 256 byte array that stores the index+1
// in the search string for each character found, == 0 if not in search
memset(fr, 0, 256);
for (i = 0; i < strlen(sl); i++) {
if (i && sl[i] == '-') { // got a range
assert(i + 1 < strlen(sl) && lstc <= sl[i + 1]); // sanity check
for (unsigned char c = lstc + 1; c <= sl[i + 1]; c++) {
fr[c] = (unsigned char) ++flen;
}
i++;
lstc = '\0';
}
else {
lstc = sl[i];
fr[sl[i]] = (unsigned char) ++flen;
}
}
unsigned int rlen;
// build replacement list
if ((rlen = strlen(rl)) != 0) {
for (i = 0; i < rlen; i++) {
if (i && rl[i] == '-') { // got a range
assert(i + 1 < rlen && t[t.length() - 1] <= rl[i + 1]); // sanity check
for (char c = t[i - 1] + 1; c <= rl[i + 1]; c++)
t += c;
i++;
}
else
t += rl[i];
}
}
// replacement string that is shorter uses last character for rest of
// string
// unless the delete option is in effect or it is empty
while (!dflg && rlen && flen > t.length()) {
t += t[t.length() - 1]; // duplicate last character
}
rlen = t.length(); // length of translation
// string
// do translation, and deletion if dflg (actually falls out of length of t)
// also squeeze translated characters if sflg
SPString tmp; // need this in case dflg, and string changes
// size
for (i = 0; i < (unsigned int) length(); i++) {
unsigned int off;
if (cflg) { // complement, ie if NOT in
// f
char rc = (!dflg && t.length() ? t[t.length() - 1] : '\0'); // always use last
// character for
// replacement
if ((off = fr[(*this)[i]]) == 0) { // not in map
cnt++;
if (!dflg && (!sflg || tmp.length() == 0 || tmp[tmp.length() - 1] != rc))
tmp += rc;
}
else
tmp += (*this)[i]; // just stays the same
}
else { // in fr so substitute with
// t, if no equiv in t then
// delete
if ((off = fr[(*this)[i]]) > 0) {
off--;
cnt++;
if (rlen == 0 && !dflg && (!sflg || tmp.length() == 0 || tmp[tmp.length() - 1] != (*this)[i]))
tmp += (*this)[i]; // stays the same
else if (off < rlen && (!sflg || tmp.length() == 0 || tmp[tmp.length() - 1] != t[off]))
tmp += t[off]; // substitute
}
else
tmp += (*this)[i]; // just stays the same
}
}
//.........这里部分代码省略.........