Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confusion about abstract strings

15 views
Skip to first unread message

jeremy...@gmx.ch

unread,
Apr 4, 2013, 1:10:11 PM4/4/13
to
Hello,

I'm a bit confused when it comes to abstract string classes, i.e. nsAString. The Mozilla internal string guide speaks of them as "historic", so I wondered that they are still expected as input value in nsDOMTokenList::Contains

In nsScriptLoader::ProcessScriptElement I want to do the following:

nsCOMPtr<nsIContentSecurityPolicy> csp;

nsAString& str1 = NS_LITERAL_STRING("string1");
nsAString& str2 = csp->getMyStringValue();

str1.Append(str2);

// call Contains(str1) now, to check if the string "string1string2"
// is a value of an element attribut

Explanation:

- getMyStringValue() is supposed to be a JavaScript function written
in contentSecurityPolicy.js and delivers a value from CSPRep, very
smililar to what getAllowsEval does with _allowEval. The returned value
is always a mixed string consisting of upper and lower case letters and
numbers, e.g. "Abc123"

- after the Append operation str1 should contain "string1string2"

Question 1: My above handling of this issue seems to be erroneous. How do I
use the strings correctly inside this context?

Question 2: nsDOMTokenList defines Contains(const nsAString& aToken,
ErrorResult& aError). However, I found this function nowhere used
in this way. Can I simply call it via Contains(str1) as it is
done in lines 111 or 148?

Thanks for your support.
Jeremy



Josh Matthews

unread,
Apr 4, 2013, 1:45:49 PM4/4/13
to mozilla-de...@lists.mozilla.org
The abstract string types should be used only for function arguments.
Concrete types such as nsString and nsAutoString should be used for new
instances of strings.

Cheers,
Josh

Boris Zbarsky

unread,
Apr 4, 2013, 2:00:57 PM4/4/13
to mozilla-de...@lists.mozilla.org
On 4/4/13 1:10 PM, jeremy...@gmx.ch wrote:
> nsAString& str1 = NS_LITERAL_STRING("string1");

I would really hope this doesn't compile....

> nsAString& str2 = csp->getMyStringValue();
>
> str1.Append(str2);

What you want is more like:

nsAutoString str1(NS_LITERAL_STRING("string1"));
str1.Append(csp->getMyStringValue());

> Question 2: nsDOMTokenList defines Contains(const nsAString& aToken,
> ErrorResult& aError). However, I found this function nowhere used
> in this way.

It's called that way from DOMTokentListBinding.cpp, which is
autogenerated from DOMTokenList.webidl.

> Can I simply call it via Contains(str1) as it is
> done in lines 111 or 148?

_If_ you are 100% sure that your string is nonempty and does not contain
whitespace, you can just call the XPCOM version of Contains() and ignore
the return value. Or call the above WebIDL version and ignore the
ErrorResult, of course.

-Boris

jeremy...@gmx.ch

unread,
Apr 5, 2013, 4:03:09 AM4/5/13
to
So, as a general rule, when a function expects a nsAString as argument, should I always use nsAutoString or can I choose from a wider range of string types because nsAString is abstract? I found that nowhere.

Jeremy

Josh Matthews

unread,
Apr 5, 2013, 9:52:50 AM4/5/13
to mozilla-de...@lists.mozilla.org
nsAString references will accept any concrete string class (such as
nsString, nsAutoString, nsDependentString, and nsDependentJSString) as
well as other references to nsAString.

Cheers,
Josh

jeremy...@gmx.ch

unread,
Apr 5, 2013, 10:06:55 AM4/5/13
to
Ok, thanks.

Jeremy
0 new messages