這里有一個文檔。
http://www.chromium.org/developers/chromium-string-usage
String的種類
在Chromium的源碼里面舟山,主要使用std::string和string16略吨,Webkit使用基于std::string的WTF::string脂男。同樣也會使用 StringPiece這個類发皿,是一個指針,指針指向的字符穿長度形成了token炒考,同樣還有WebCString和WebString可缚,在webkit glue layer之中。
String的編碼
chromium本身使用了很多編碼種類斋枢。UTF-8是最通用的帘靡,同樣也使用UTF-16和UCS-2以及其他
UTF-8
什么時候使用哪種編碼
最關(guān)鍵的規(guī)則是meta-rule,周邊代碼的編碼風(fēng)格瓤帚。在前端描姚,我們使用utf-8作為std::string/char以及使用UTF-16作為string16/char16的編碼,基于std:string是編碼不可知的戈次,所以我們只把utf-8放進去轩勘。std::wstring/wchar_t只能用在windows系統(tǒng)的本地api里面,因為在不同的平臺的長度不同怯邪,大部分的UI string是UTF-16的绊寻,而url是UTF-8,字符串在webkit glue層是UTF-16
關(guān)于GURL
一個最常用的數(shù)據(jù)類型是GURL類,它的構(gòu)造函數(shù)輸入UTF-8編碼的std::string作為它本身的URL,你可以使用spec()方法來拿到整個url的std:string澄步,或者你可以使用其他方法來獲得url的其他部分冰蘑,比如scheme(),host()等
Guidelines for string use in our codebase
chromium代碼庫的字符串使用指南
- Use std::string from the C++ standard library for normal use with strings
在普通用法的時候,使用std::string就好了 - 檢查長度——如果檢查是否為空驮俗,比起使用"string.length() ==0"懂缕,最好使用"string.empty()"
- When you make a string constant at the top of the file, use char[] instead of a std::string:
當你在文件的頭部使用字符常量的時候,使用char[]王凑,而不是std::string搪柑,例如
const char kFoo[] = "foo"
ex) const char kFoo[] = “foo”;
這是我們指南的一部分,這種方式因為沒有析構(gòu)過程所以更快索烹,另外代碼也更加可維護因為沒有shutdown的順序依賴工碾。
There are many handy routines which operate on strings. You can use IntToString() if you want to do atoi(), and StringPrintf() if you need the full power of printf. You can use WriteInto() to make a C++ string writeable by a C API. StringPiece makes it easy and efficient to write functions that take both C++ and C style strings.
- 在string的操作方面,有一些可操作的規(guī)范百姓。如果你想用atoi()渊额,你能用IntToString(),你想要打印全面的花垒拢,你可以嘗試一下StringPrintf()旬迹。如果你想要C++的string被C的API可寫入的話,可以使用WriteInto()求类。如果函數(shù)又涉及C++和C風(fēng)格的string奔垦,那么StringPiece會更合適。
For function input parameters, prefer to pass a string by const reference instead of making a new copy.
- 函數(shù)的輸入?yún)?shù)尸疆,更適合傳進來一個字符串的常量引用而不是新的拷貝椿猎。
For function output parameters, it is OK to either return a new string or pass a pointer to a string. Performance wise, there isn’t much difference.
- 在函數(shù)的輸出參數(shù)方面,返回新的字符串或者傳一個字符串的指針都是OK的寿弱,在性能上也沒有什么太大差距犯眠。
Often, efficiency is not paramount, but sometimes it is - when working in an inner loop, pay special attention to minimize the amount of string construction, and the number of temporary copies made.
- 經(jīng)常情況下,效率不是最重要的症革,但是有的時候還是很重要的--當在一個內(nèi)循環(huán)里面筐咧,需要特別注意減少string的構(gòu)造,以及臨時拷貝的數(shù)目噪矛。
When you use std::string, you can end up constructing lots of temporary string objects if you aren’t careful, or copying the string lots of times. Each copy make a call to malloc, which needs a lock, and slows things down. Try to minimize how many temporaries get constructed.
當你使用std::string的時候量蕊,你可能會不注意構(gòu)造大量的臨時字符串對象或者拷貝很多次字符串,每次拷貝會調(diào)用malloc摩疑,它需要上鎖危融,導(dǎo)致變慢畏铆。盡量減少臨時變量被構(gòu)造的次數(shù)雷袋。
When building a string, prefer “string1 += string2; string1 += string3;” to “string1 = string1 + string2 + string3;” Better still, if you are doing lots of this, consider a string builder class.
在構(gòu)造字符串的時候,盡量 “string1 += string2; string1 += string3;” 而不是“string1 = string1 + string2 + string3;”,更好的方案是楷怒,在你需要很多這樣的操作的時候蛋勺,考慮字符串構(gòu)造類。
For localization, we have the ICU library, with many useful helpers to do things like find word boundaries or convert to lowercase or uppercase correctly for the current locale.
為了本地化鸠删,我們有ICU庫抱完,有很多有用的helper,可以用來做類似找到單詞邊界刃泡,轉(zhuǎn)化大小寫之類的巧娱。
We try to avoid repeated conversions between string encoding formats, as converting them is not cheap. It's generally OK to convert once, but if we have code that toggles the encoding six times as a string goes through some pipeline, that should be fixed.
我們嘗試避免重復(fù)在不同的字符串編碼格式中轉(zhuǎn)化,因為轉(zhuǎn)化的成本很高烘贴。當然你轉(zhuǎn)化一次是OK的禁添,但是如果一個字符串在一個流程中被反復(fù)轉(zhuǎn)化六次之類的,應(yīng)當被修改桨踪。