Chromium的string打印

2017-10-22  本文已影响163人  进击的前端

这里有一个文档。
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代码库的字符串使用指南

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.

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.

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.

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

上一篇 下一篇

猜你喜欢

热点阅读