📄 jstrings_cc.cpp
字号:
//---------------------------------------------------------------------------
//-------- JStrings_C.cpp ---------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
#include "JStrings_H.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 一个增强的字串类。
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 下面是实现部分
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#define Renew(var,typ); delete[] var; var = new typ;
#define NewChar(var,len); var = new char[len];
#define ReNewChar(var,len); Renew(var,char[len])
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 将字串首尾的字符ch去除(如果ch存在的话)。
//---------------------------------------------------------------------------
void JStrings::Trim(char ch)
{ if(Length()<2 ||*p !=ch || LastChar()!=ch ) { return; } // endif
DelTail();
DelHead();
} // end TrimDbl
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 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<size-2; i++ ) \
{ if(!(condition)) { return NotValueStr; }} \
return judge; \
}
//---------------------------------------------------------------------------
int8u JStrings::IsValueStr()
{ if( !p || !FirstCharDigit() ) // 如果p是空的,或者首字符不是数字,返回0。
{ return NotValueStr; } // endif
// Now, 第一个字母都是数字
register int16u i;
switch( *(int16u*)p )
{ case '0x' : case '0X' :
for( i = 2; i < Length(); 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 JStrings::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'
{ p[size-2] = '\0';
value = TransToint32u(radix);
p[size-2] = ch;
} // end else
} // endif
return radix;
} // end ValueStrToValue
//---------------------------------------------------------------------------
//-------- ( for Debug ) ----------------------------------------------------
// 判断字串是否数字串,是则显示出相应的信息。否则什么都不做。
//---------------------------------------------------------------------------
void JStrings::TellmeStrtype()
{ register int8u radix = IsValueStr();
switch(radix)
{ case NotValueStr: { cout<<"\nNot Value String."; break; }
case BinValueStr: { cout<<"\nBin Value String."; break; }
case DecValueStr: { cout<<"\nDec Value String."; break; }
case OctValueStr: { cout<<"\nOct Value String."; break; }
case HexValueStr: { cout<<"\nHex Value String."; break; }
default: { cout<<"\nError in TellmeStrtype()."; } // end default
} // end switch
} // end TellmeStrtype
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 字符迁移宏。
//---------------------------------------------------------------------------
#define forwardMov(t); \
while( *(t) ) { *(t)=*( (t)+1 ); (t)++; }
//---------------------------------------------------------------------------
// 把字串中的'$'号去掉。从后面开始扫描。
//---------------------------------------------------------------------------
void JStrings::dollarCat()
{ if(size > 1)
{ char* f = p + size - 2; // point to the last char
while( f != p )
{ if( *f == '$' ) { forwardMov(f); } // endif
f--;
} // end while
size = strlen(p) + 1; // update size
} // endif
} // end dollarCat
//---------------------------------------------------------------------------
#undef forwardMov();
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 把字串中的某个字符删除,注意,f指向的字符就是所删字符,它必须在字串里!
//---------------------------------------------------------------------------
void JStrings::DelCharInStr(char* f) // 此函数要慎用!
{ if(*f) { --size; } // update size
while(*f) { *(f-1) = *(++f); } // end while
} // end DelCharInStr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 用字串中的字串替换本身。注意,f指向的就是新串,它必须在字串里!
//---------------------------------------------------------------------------
void JStrings::ReplaceBySubstr(char* f) // 此函数要慎用!
{ register int16u i = 0;
for( ; f[i] != '\0'; ++i ) { p[i] = f[i]; } // end for
p[i] = '\0';
size = i; // update size
} // end ReplaceBySubstr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 将字串首尾的空格去除。
//---------------------------------------------------------------------------
void JStrings::NoBlankInStr()
{ while(LastChar() == ' ') { DelTail(); } // end while
while(FirstChar() == ' ') { DelCharInStr(p); } // end while
} // end NoBlankInStr
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#undef Renew(var,typ);
#undef NewChar(var,len);
#undef ReNewChar(var,len);
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// end JStrings_C.cpp
//---------------------------------------------------------------------------
// Written by JamesyFront. ZLGmcu Dev.Co.Ltd. 2002.
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -