Since we support multiple locales, we have to be extra careful about using string comparison and sort operations. (NOTE: this only applies to server apps, not desktop apps such as the connectors).
Unfortunately, .NET did not make things easy and default to the behavior we DON'T want in our server app. Namely, it's using the application culture instead of the invariant culture. :(
Here are the rules to follow when working with strings. If you're now sure what to use, just ping me on skype/irc.
1) Comparing generic strings:
DON'T USE:
string.Compare(a, b)
USE:
StringUtil.CompareInvariant(a, b)
DON'T USE:
string.Compare(a, b, true)
USE:
StringUtil.CompareInvariantIgnoreCase(a, b)
DON'T USE:
a.StartsWith(b)
USE:
StringUtil.StartsWithInvariant(a, b)
(ditto for EndsWith, IndexOf, and LastIndex Of)
DON'T USE
a == b
USE:
StringUtil.EqualsInvariant(a, b)
DON'T USE
a.ToLowerInvariant() == "foo"
USE:
StringUtil.EqualsInvariantIgnoreCase(a, "foo")
2) Comparing content order:
USE (non-case sensitive):
string.Compare(a, b, false, DreamContext.Current.Culture)
USE (case sensitive):
string.Compare(a, b, true, DreamContext.Current.Culture)
3) Using regular expressions:
USE:
new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant);
NEVER use StringComparison.Ordinal unless you know what you're doing (e.g. for comparing passwords use string.CompareOrdinal)
NEVER EVER use StringComparison.OrdinalIgnoreCase because its implementation is messed up
What is the difference between Invariant and Ordinal? Good question!
* Invariant is the US-English locale, but it doesn't change when the current culture changes.
* Ordinal does a binary comparison; so, although, two characters might be logically identical, the comparison will fail if their binary values are not.
- Steve
| Images 0 | ||
|---|---|---|
| No images to display in the gallery. |
Copyright © 2011 MindTouch, Inc. Powered by