빌더 프로그래밍을 처음 시작하시는 분이 제일 처음으로 마주치는 문제 중의 하나는,
VCL/CLX가 기본으로 제공하는 문자열 클래스인 AnsiString을 제대로 이해하는 것입니다.
이에 관련된 두가지 팁을 올려드리겠습니다.
이 글은 그 첫번째 글입니다.
=========================================================================================
ANSI C++ STL의 string(basic_string)에는
find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of()와 같은 멤버 함수들이 있어서, 토크나이저(tokenizer)와 같은 기능들을 손쉽게 구현할 수 있습니다.
오버로딩된 함수의 종류도 다음과 같이 다양합니다.
size_type find_first_of(const basic_string&, size_type = 0) const;
size_type find_first_of(const charT*, size_type, size_type) const;
size_type find_first_of(const charT*, size_type = 0) const;
size_type find_first_of(charT, size_type = 0) const;
size_type find_last_of(const basic_string&, size_type = npos) const;
size_type find_last_of(const charT*, size_type, size_type) const;
size_type find_last_of(const charT*, size_type = npos) const;
size_type find_last_of(charT, size_type = npos) const;
size_type find_first_not_of(const basic_string&, size_type = 0) const;
size_type find_first_not_of(const charT*, size_type, size_type) const;
size_type find_first_not_of(const charT*, size_type = 0) const;
size_type find_first_not_of(charT, size_type = 0) const;
size_type find_last_not_of(const basic_string&, size_type = npos) const;
size_type find_last_not_of(const charT*, size_type, size_type) const;
size_type find_last_not_of(const charT*, size_type = npos) const;
size_type find_last_not_of(charT, size_type = npos) const;
반면에 VCL/CLX의 AnsiString에는
LastDelimiter() 한 가지 밖에 없으며
함수 종류도 멤버함수/일반함수가 각 한가지 밖에 없습니다.
int __fastcall LastDelimiter(const AnsiString& delimiters) const; // 멤버함수
int __fastcall LastDelimiter(const AnsiString Delimiters, const AnsiString S); // 일반함수
(원하는 위치부터 검색하는 기능도 빠져있습니다. -_-;)
다행히도 AnsiString에는 IsDelimiter()라는 함수가 있기 때문에
bool __fastcall IsDelimiter(const AnsiString& delimiters, int index) const; // 멤버함수
bool __fastcall IsDelimiter(const AnsiString Delimiters, const AnsiString S, int Index); // 일반함수
FirstDelimiter(), FirstNotDelimiter(), LastDelimiter(), LastNotDelimiter()
와 같은 함수들을 비교적 쉽게 구현할 수 있습니다.
-------------------------------------------------------------------------------------------
int FirstDelimiter(const AnsiString& Delimiters, const AnsiString& S)
{
int n = S.Length();
for (int i = 1; i <= n; i++)
if (S.IsDelimiter(Delimiters, i))
return i;
return n + 1;
}
int FirstDelimiter(const AnsiString& Delimiters, int Pos, const AnsiString& S)
{
int n = S.Length();
for (int i = Pos; i <= n; i++)
if (S.IsDelimiter(Delimiters, i))
return i;
return n + 1;
}
int LastDelimiter (const AnsiString& Delimiters, int Pos, const AnsiString& S)
{
for (int i = Pos; i >= 1; i--)
if (S.IsDelimiter(Delimiters, i))
return i;
return 0;
}
int FirstNotDelimiter(const AnsiString& Delimiters, const AnsiString& S)
{
int n = S.Length();
for (int i = 1; i <= S.Length(); i++)
if (!S.IsDelimiter(Delimiters, i))
return i;
return n + 1;
}
int FirstNotDelimiter(const AnsiString& Delimiters, int Pos, const AnsiString& S)
{
int n = S.Length();
for (int i = Pos; i <= S.Length(); i++)
if (!S.IsDelimiter(Delimiters, i))
return i;
return n + 1;
}
int LastNotDelimiter (const AnsiString& Delimiters, const AnsiString& S)
{
for (int i = S.Length(); i >= 1; i--)
if (!S.IsDelimiter(Delimiters, i))
return i;
return 0;
}
int LastNotDelimiter (const AnsiString& Delimiters, int Pos, const AnsiString& S)
{
for (int i = Pos; i >= 1; i--)
if (!S.IsDelimiter(Delimiters, i))
return i;
return 0;
}