Platform/Fix Strings

From MozillaWiki
Jump to: navigation, search

The general problems we're trying to solve

The strings we're talking about here are the internal string API.

Problems we would like to solve:

  • Many common tasks are harder than necessary to do with the current API. We need to add simple API covering simple common tasks.
  • Documentation is lacking

We're not out here to replace string classes by different ones: that would be near impossible anyway, and there's lots of good stuff in our current string classes. They just need attention.

New API for common tasks

String assembly

We have a really smart operator+ for efficient string concatenation, and we have AppendPrintf. What is missing:

Type-generic operator+

Printf-style is nice to have, but not optimal. It's not extensible, not the most readable, not type-safe and not super efficient as it has to parse its format string. Since we already have very smart and efficient string concatenation with operator+ and nsSubstringTuple, it would be nice to extend it to support other types than just strings.

The API should be as neat as

 result = some_string + some_integer + other_string + some_float;

This can actually be done from the outside, as operator+ doesn't need to be a class member, but it would be nice if at least common types (like numbers) were working out-of-the-box.

Moreover, while operator+'s can be added from the outside, the same is not true of assignment and compound-assignment operators like +=.

So it would be nice if some generic operator=, operator+, operator+= were added to the string classes themselves, in a way that make them extensible to new types. For example, they could all use a standardized ConvertToString<T>::run(T) function so that extending support to a new type T would only require providing a template specialization ConvertToString<T>.

String builder: Already implemented

Jeff has already landed (bug 711491) his StringBuilder class in xpcom/ds.

NS_LossyConvertUTF16toASCII and friends

Half of Gecko developers don't know that it's actually a class. For good reason: the "Convert" verb in its name suggest an action, i.e. a function rather than a class. Also, the rather unusual NS_ prefix doesn't help.