提供兩個包裝 Multibyte 和 Unicode 的字串指標 MB_String 及 UNICODE_String,都繼承自 Smart_Ptr,除了一般字串的處理外, Multibyte 和 Unicode 之間還可以互相轉換,但記得要先用 setlocale() 設定好正確的地區語言。
Multibyte 包裹 string_wrapper<char> , Unicode 包裹 string_wrapper<wchar_t>,string_wrapper 繼承自 cxxlObject,目的是要讓 char 和 wchar_t 陣列能有 cxxlObject 的能力。
MB_String 和 UNICODE_String 主要的建構函數如下
MB_String(const char *str, ISpirit *spirit = Spirit_Easy);
UNICODE_String(const wchar_t *str, ISpirit *spirit = Spirit_Easy);
字串是採取由 str 複製,不是用參照。
spirit 是給 string_wrapper 用的。
內定建構函數(無參數的建構函數)是包裹 NULL(沒有包裹 string_wrapper),可視為空字串,可以用 isNULL() 成員函數檢驗,若不是空字串可用 StrLength() 成員函數取得字串長度
也可建構時指定一個數值,表示要為其指定的字元個數(包含 \0 結尾字元)準備存放空間,但使用端須自行填入資料。
MB_String 和 UNICODE_String 可和 Smart_Ptr 互通使用 string_wrapper,但 string_wrapper 只是包裹字串,須放入 MB_String 或 UNICODE_String 才會有額外的字串功能
範例一
#include <iostream>
#include <locale>
#include <SMART_STRING.HPP>
using namespace std;
using namespace CxxlMan;
#include "stdafx.h" // 因是在 vs2008 做的範例,所以會有這個
int _tmain(int argc, _TCHAR* argv[])
{
string str_in;
cout << "輸入 Multibyte 字串: ";
cin >> str_in;
MB_String M_Str = str_in.c_str();
setlocale(LC_CTYPE, ""); // 指定系統現正使用中的地區語言做轉換
UNICODE_String U_Str = M_Str; // 轉換成 Unicode
wcout << L"轉換成 Unicode 字串: " << (const wchar_t*)U_Str << endl;
system("pause");
}
字串功能
MB_String 和 UNICODE_String 擴增了以下 Smart_Ptr 沒法提供的功能,要使用這些功能必須不能為空字串(包裹 NULL),否則一定會當
Multibyte 和 Unicode 互相轉換 |
|
可用陣列註標 [ ] 的方法處理單一字元 |
|
字串比對 |
有 ==、!=、>、<、>=、<=,大小寫有別 |
字串連結 |
使用 + 運算子 |
增加字串 |
使用 += 運算子 |
字串長度 |
使用 StrLength() 成員函數 |
int _tmain(int argc, _TCHAR* argv[])
{
char *Src = "這是 MB 字串。";
MB_String M_Str1; // 空字串,包裹的是 NULL,沒法使用
M_Str1 = Src;
Smart_Ptr<string_wrapper<char> > M_Ptr1 = M_Str1; // Smart_Ptr 的用法
// 若要指定 Spirit_Urgent 須用顯示建構
MB_String M_Str2(Src,rootObject,Spirit_Urgent);
cout << "M_Str1: " << M_Str1 << endl
<< "M_Str2: " << M_Str2 << endl
// Smart_Ptr 沒提供 (char*) 的轉形,叫出 string_wrapper<char> 來做
<< "M_Ptr1: " << *M_Ptr1 << endl << endl;
// 測試字串連結
M_Str1 += "It's good. ";
cout << "M_Str1: " << M_Str1 << endl;
M_Str1 = M_Str1 + "好啦! " + "我相信你";
cout << "M_Str1: " << M_Str1 << endl << endl;
// 字串連結沒法指定 Spirit_Urgent,若希望使用 Spirit_Urgent
// 得用這樣複製一次
M_Str1 = MB_String(M_Str1,rootObject,Spirit_Urgent);
// 轉型測試
setlocale(LC_CTYPE, ""); // 指定系統現正使用中的地區語言做轉換
UNICODE_String U_Str1 = M_Str2;
UNICODE_String U_Str2(M_Str2,rootObject,Spirit_Urgent); // 為了指定使用 Spirit_Urgent
wcout << "U_Str1: " << (const wchar_t*)U_Str1 << endl
<< "U_Str2: " << (const wchar_t*)U_Str2 << endl << endl;
// 比對測試
if(U_Str2 != "這是 UNICODE 字串。")
U_Str2 = "這是 UNICODE 字串。";
wcout << "U_Str2: " << (const wchar_t*)U_Str2 << endl << endl;
system("pause");
}
一般字串物件的內容是在建構時由提供的來源字串複製而來,若你想自己處理其內容,可以在建構時指示要用的字元數,就只會備好你要的存放空間,再經由轉形取得這個空間的位置,就可以自行處理其內容了,注意!這方法只應用在建構時,若在使用時改變內容在多線程式中會造成干擾
#include <iostream>
#include <SMART_STRING.HPP>
using namespace std;
using namespace CxxlMan;
int main()
{
MB_String MB_Str(21); // 只配置空間未初始內容
const char *p = MB_Str; // 取得配置的空間
for(int i = 0; i < 21; ++i,++p)
{
*(char *)p = ('A' + i);
}
*(char *)p = '\0';
cout << (const char*)MB_Str << endl;
return 0;
}
在 r92 版(舊版 svn 序號)之後,MB_String 和 UNICODE_String 在架構上有些改變,在 比較 和 字串連結 的操作功能更容易使用,不過以往 char * 至 UNICODE_String 和 wchar_t * 至 MB_String 的建構功能取消了,改用 MB_String 至 UNICODE_String 和 UNICODE_String 至 MB_String ,有些編譯器會有沒法自動轉型(如 gcc),所以以下用法要改變一下
// gcc 沒法自動完成
UNICODE_String Str = "abc";
MB_String Str = L"abc";
// 改成這樣用
UNICODE_String Str( "abc");
MB_String Str(L"abc");
void f1(const UNICODE_String &Str);
void f2(const MB_String &Str);
// gcc 沒法自動完成
f1("abc);
f2(L"abc");
// 改成這樣用
f1(L"abc");
f1(MB_String("abc"));
f1(MB_String(L"abc"));
f1(UNICODE_String(L"abc"));
f1(UNICODE_String("abc"));
f2("abc");
f2(MB_String("abc"));
f2(MB_String(L"abc"));
f2(UNICODE_String(L"abc"));
f2(UNICODE_String("abc"));