這里有一個文檔。
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
什么時候使用哪種編碼
最關鍵的規則是meta-rule,周邊代碼的編碼風格。在前端,我們使用utf-8作為std::string/char以及使用UTF-16作為string16/char16的編碼,基于std:string是編碼不可知的,所以我們只把utf-8放進去。std::wstring/wchar_t只能用在windows系統的本地api里面,因為在不同的平臺的長度不同,大部分的UI string是UTF-16的,而url是UTF-8,字符串在webkit glue層是UTF-16
關于GURL
一個最常用的數據類型是GURL類,它的構造函數輸入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”;
這是我們指南的一部分,這種方式因為沒有析構過程所以更快,另外代碼也更加可維護因為沒有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的操作方面,有一些可操作的規范。如果你想用atoi(),你能用IntToString(),你想要打印全面的花,你可以嘗試一下StringPrintf()。如果你想要C++的string被C的API可寫入的話,可以使用WriteInto()。如果函數又涉及C++和C風格的string,那么StringPiece會更合適。
For function input parameters, prefer to pass a string by const reference instead of making a new copy.
- 函數的輸入參數,更適合傳進來一個字符串的常量引用而不是新的拷貝。
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.
- 在函數的輸出參數方面,返回新的字符串或者傳一個字符串的指針都是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.
- 經常情況下,效率不是最重要的,但是有的時候還是很重要的--當在一個內循環里面,需要特別注意減少string的構造,以及臨時拷貝的數目。
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的時候,你可能會不注意構造大量的臨時字符串對象或者拷貝很多次字符串,每次拷貝會調用malloc,它需要上鎖,導致變慢。盡量減少臨時變量被構造的次數。
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.
在構造字符串的時候,盡量 “string1 += string2; string1 += string3;” 而不是“string1 = string1 + string2 + string3;”,更好的方案是,在你需要很多這樣的操作的時候,考慮字符串構造類。
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,可以用來做類似找到單詞邊界,轉化大小寫之類的。
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.
我們嘗試避免重復在不同的字符串編碼格式中轉化,因為轉化的成本很高。當然你轉化一次是OK的,但是如果一個字符串在一個流程中被反復轉化六次之類的,應當被修改。