📄 jstring_h.h
字号:
//---------------------------------------------------------------------------
// 字串加法运算符。字串自身不改变。
// Example: obj_c = obj_a + obj_b;
//---------------------------------------------------------------------------
inline Jstring Jstring::operator+(const Astring& obj_b)const
{ if(obj_b.IsEmpty()) // 如果obj_b空
{ return *this; } // endif
else // obj_b不空
{ Jstring temp; // temp.p 还没被new。
register size_t alen = this->getLen();
temp.p = new char[alen + obj_b.getLen() + 1];
memcpy(temp.p, p, alen);
strcpy(temp.p + alen, (const char*)obj_b);
return temp;
} // end else
} // end operator+ -- for obj
//---------------------------------------------------------------------------
//------ Friend Function operator+ ------------------------------------------
// obj_c = str + obj_b
//---------------------------------------------------------------------------
inline Jstring operator+(const char *s, const Jstring& obj)
{ Jassert(s!=NULL);
if(*s == '\0')
{ Jstring temp(obj); return temp; } // endif
if(obj.IsEmpty())
{ Jstring temp(s); return temp; } // endif
Jstring temp;
register size_t slen = strlen(s);
temp.p = new char[slen + obj.getLen() + 1];
memcpy(temp.p, s, slen);
strcpy(temp.p + slen, obj.p);
return temp;
} // end friend operator+
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 把字串中的'$'号去掉。从后面开始扫描。但是首字符不删。
//---------------------------------------------------------------------------
void Jstring::dollarCat()
{ register char* f = p + getLen(); // point to the last char
while( f != p )
{ if( *f == '$' )
{ while(*f) { *(f-1) = *(++f); } // end while It is the old flavour.
} // endif // 字符迁移
--f;
} // end while
} // end dollarCat
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 把字串中的某个字符删除。
// 注意,f指向的字符就是所删字符,它必须在字串里!字串不应该是空串。
//---------------------------------------------------------------------------
void Jstring::DelCharInStr(size_t n) // 此函数要慎用!
{ register char* f = p + n;
Jassert(f>=p && f<p+getLen());
while(*f) { *(f-1) = *(++f); } // end while It is the old flavour.
} // end DelCharInStr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 用字串中的字串替换本身。
// 注意,f指向的就是新串,它必须在字串里!字串不应该是空串。
//---------------------------------------------------------------------------
void Jstring::ReplaceBySubstr(size_t n) // 此函数要慎用!
{ register char* f = p + n;
Jassert(f>=p && f<p+getLen());
register size_t i = 0;
for( ; f[i] != '\0'; ++i ) { p[i] = f[i]; } // end for
p[i] = '\0';
} // end ReplaceBySubstr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 将字串首尾的空格去除。字串不应是空串。
//---------------------------------------------------------------------------
void Jstring::NoBlankInStr()
{ Jassert(p!=NULL && *p!='\0');
register char* t = p + getLen() - 1; // 移到 last char。
while(*t == ' ') { *t = '\0'; --t; } // end while
while(FirstChar() == ' ')
{ while(*p) { *(p-1) = *(++p); } // end while It is the old flavour.
} // end while
} // end NoBlankInStr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 将字串首尾的字符ch去除(如果首尾字符ch都存在的话)。
//---------------------------------------------------------------------------
inline void Jstring::Trim(char ch)
{ if(getLen()<2 ||*p !=ch || LastChar()!=ch ) { return; } // endif
DelTail();
DelHead();
} // end Trim
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// s 已经移到一个数字上
// 如果不是数字串,返回0; 如果是数串,返回非零值
// 如果是二进制数字串 如0101101B,10100110b,返回2
// 如果是八进制数字串,如023754O, 77777o, 返回8
// 如果是十进制数字串,如012789, 98321D,999d,返回10
// 如果是十六进制数串,如0FFAH, 342ebh, 返回16
//---------------------------------------------------------------------------
//#pragma warn -8098
const int8u NotValueStr = 0;
const int8u BinValueStr = 2;
const int8u OctValueStr = 8;
const int8u DecValueStr = 10;
const int8u HexValueStr = 16;
//---------------------------------------------------------------------------
#define TestValueStr(condition, judge); \
{ for( i=0; i<len-1; ++i ) \
{ if(!(condition)) { return NotValueStr; }} \
return judge; \
}
//---------------------------------------------------------------------------
int8u Jstring::IsValueStr()const
{ if( !p || !IsFirstCharDigit() ) // 如果p是空的,或者首字符不是数字,返回0。
{ return NotValueStr; } // endif
// Now, 第一个字母都是数字
register size_t i;
size_t len = getLen();
switch( *(const int16u*)p )
{ case '0x' : case '0X' :
for( i = 2; i < len; ++i )
{ if(!isxdigit(p[i])) return NotValueStr; } // end for
return HexValueStr;
// This switch is of no default.
} // end switch
register char ch = LastChar();
if( isdigit(ch) )
{ TestValueStr( isdigit(p[i]), DecValueStr ); } // endif
else // 末尾不是数字
{ switch(ch)
{ case 'H': case 'h':
TestValueStr( isxdigit(p[i]), HexValueStr );
case 'B': case 'b':
TestValueStr( p[i]=='0' || p[i]=='1' , BinValueStr );
case 'D': case 'd':
TestValueStr( isdigit(p[i]), DecValueStr );
case 'O': case 'o': case 'Q': case 'q':
TestValueStr( p[i]>='0' && p[i]<='7' , OctValueStr );
} // end switch
} // end else
return NotValueStr;
} // end IsValueStr
//---------------------------------------------------------------------------
#undef TestValueStr();
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 如果是数串,输出它的值到value。
// 如果不是数字串,返回0; 如果是数串,返回非零值
// 如果是二进制数字串,返回2
// 如果是八进制数字串,返回8
// 如果是十进制数字串,返回10
// 如果是十六进制数串,返回16
//---------------------------------------------------------------------------
int8u Jstring::ValueStrToValue(int32u &value)
{ value = 0;
dollarCat(); // 把其中的'$'号去掉.
int8u radix = IsValueStr(); // 找出数字串的radix,如果radix==0,它不是数串
if(radix) // 如果它是数串(radix!=0)
{ char ch = LastChar();
if(ch<='9' || (radix==HexValueStr && ch!='H' && ch!='h') )
{ value = TransToint32u(radix); } // endif
else // 最后字符不是数字, 必是'H','B','O','D'
{ size_t last = getLen()-1;
p[last] = '\0';
value = TransToint32u(radix);
p[last] = ch;
} // end else
} // endif
return radix;
} // end ValueStrToValue
//---------------------------------------------------------------------------
//-------- ( for Debug ) ----------------------------------------------------
// 判断字串是否数字串,是则显示出相应的信息。否则什么都不做。
//---------------------------------------------------------------------------
void Jstring::TellmeStrtype()const
{ register int8u radix = IsValueStr();
switch(radix)
{ case NotValueStr: { printf("\nNot Value String."); break; }
case BinValueStr: { printf("\nBin Value String."); break; }
case DecValueStr: { printf("\nDec Value String."); break; }
case OctValueStr: { printf("\nOct Value String."); break; }
case HexValueStr: { printf("\nHex Value String."); break; }
default: { printf("\nError in TellmeStrtype()."); } // end default
} // end switch
} // end TellmeStrtype
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
// Written by JamesyFront. ZLGmcu Dev.Co.Ltd. 2002.
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -