这里有一个文档。
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的,但是如果一个字符串在一个流程中被反复转化六次之类的,应当被修改。