diff --git a/mozilla/string/doc/string-guide.html b/mozilla/string/doc/string-guide.html index 99e3395205a..dcf02dc3c3a 100644 --- a/mozilla/string/doc/string-guide.html +++ b/mozilla/string/doc/string-guide.html @@ -5,7 +5,6 @@ -
@@ -144,6 +143,7 @@| + | you have some chars | +||||
|---|---|---|---|---|---|
| you want | +'x' | +char c | +"foo" | +char* cp | +nsACString& cs | +
| char | +- | +- | +[] | +[] | +how to extract a character | +
| PRUnichar | +PRUnichar('x') | +PRUnichar(c) | ++ | + | + |
| char* | ++ | + | + | + | how to get a pointer | +
| PRUnichar* | ++ | + | + | + | + |
| nsACString | ++ | + | + | + | + |
| nsAString | ++ | + | + | + | + |
| to call printf | ++ | + | + | + | + |
| + | you have some PRUnichars | +||
|---|---|---|---|
| you want | +PRUnichar w | +PRUnichar* wp | +nsAString& s | +
| char | ++ | + | + |
| PRUnichar | ++ | + | + |
| char* | ++ | + | + |
| PRUnichar* | ++ | + | + |
| nsACString | ++ | + | + |
| nsAString | ++ | + | how to get a pointer | +
| to call printf | ++ | + | + |
+ /* I have a string, how do I get a pointer to the characters? */
+
+extern void EvilNarrowOSFunction( const char* ); // evil OS routines that want a pointers
+extern void EvilWideOSFunction( const PRUnichar* );
+
+void func( const nsAString& aString, const nsACString& aCString )
+ {
+ EvilWideOSFunction( NS_LITERAL_STRING("Hello, World!").get() );
+ // literal strings are flat already (as are |nsString|s, et al), just use |.get()|
+
+ EvilWideOSFunction( PromiseFlatString(aString).get() );
+ // for strings that don't explicitly guarantee flatness, use |PromiseFlatString|
+
+
+ // beware holding the pointer for longer than the life of the promise
+ const PRUnichar* wp = PromiseFlatString(aString).get(); // BAD! |wp| dangles
+ EvilWideOSFunction(wp);
+
+ // if you really need to use the pointer from |PromiseFlatString| in more than one expression...
+ const nsAFlatString& flat = PromiseFlatString(aString);
+ EvilWideOSFunction(flat.get());
+ SomeOtherFunction(flat.get());
+
+ // similarly for |char| strings
+ EvilNarrowOSFunction( PromiseFlatCString(aCString).get() );
+ }
+
+
+PRUnichar Get5thCharacterOf( const nsAString& aString )
+ {
+ if ( aString.Length() >= 5 )
+ {
+ nsAString::const_iterator iter;
+ aString.BeginReading(iter); // make |iter| point to the beginning of |aString|
+ iter.advance(5);
+ return *iter;
+ }
+
+ return PRUnichar(0);
+ }
+
+| + | you have some chars | +||||
|---|---|---|---|---|---|
| you want | +'x' | +char c | +"foo" | +char* cp | +nsACString& cs | +
| char | +- | +- | +[] | +[] | +how to extract a character | +
| PRUnichar | +PRUnichar('x') | +PRUnichar(c) | ++ | + | + |
| char* | ++ | + | + | + | how to get a pointer | +
| PRUnichar* | ++ | + | + | + | + |
| nsACString | ++ | + | + | + | + |
| nsAString | ++ | + | + | + | + |
| to call printf | ++ | + | + | + | + |
| + | you have some PRUnichars | +||
|---|---|---|---|
| you want | +PRUnichar w | +PRUnichar* wp | +nsAString& s | +
| char | ++ | + | + |
| PRUnichar | ++ | + | + |
| char* | ++ | + | + |
| PRUnichar* | ++ | + | + |
| nsACString | ++ | + | + |
| nsAString | ++ | + | how to get a pointer | +
| to call printf | ++ | + | + |
+ /* I have a string, how do I get a pointer to the characters? */
+
+extern void EvilNarrowOSFunction( const char* ); // evil OS routines that want a pointers
+extern void EvilWideOSFunction( const PRUnichar* );
+
+void func( const nsAString& aString, const nsACString& aCString )
+ {
+ EvilWideOSFunction( NS_LITERAL_STRING("Hello, World!").get() );
+ // literal strings are flat already (as are |nsString|s, et al), just use |.get()|
+
+ EvilWideOSFunction( PromiseFlatString(aString).get() );
+ // for strings that don't explicitly guarantee flatness, use |PromiseFlatString|
+
+
+ // beware holding the pointer for longer than the life of the promise
+ const PRUnichar* wp = PromiseFlatString(aString).get(); // BAD! |wp| dangles
+ EvilWideOSFunction(wp);
+
+ // if you really need to use the pointer from |PromiseFlatString| in more than one expression...
+ const nsAFlatString& flat = PromiseFlatString(aString);
+ EvilWideOSFunction(flat.get());
+ SomeOtherFunction(flat.get());
+
+ // similarly for |char| strings
+ EvilNarrowOSFunction( PromiseFlatCString(aCString).get() );
+ }
+
+
+PRUnichar Get5thCharacterOf( const nsAString& aString )
+ {
+ if ( aString.Length() >= 5 )
+ {
+ nsAString::const_iterator iter;
+ aString.BeginReading(iter); // make |iter| point to the beginning of |aString|
+ iter.advance(5);
+ return *iter;
+ }
+
+ return PRUnichar(0);
+ }
+
+