Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/ArrayUtils.h"
8 :
9 : #include "SVGNumberList.h"
10 : #include "nsCharSeparatedTokenizer.h"
11 : #include "nsString.h"
12 : #include "nsTextFormatter.h"
13 : #include "SVGContentUtils.h"
14 :
15 : namespace mozilla {
16 :
17 : nsresult
18 0 : SVGNumberList::CopyFrom(const SVGNumberList& rhs)
19 : {
20 0 : if (!mNumbers.Assign(rhs.mNumbers, fallible)) {
21 0 : return NS_ERROR_OUT_OF_MEMORY;
22 : }
23 0 : return NS_OK;
24 : }
25 :
26 : void
27 0 : SVGNumberList::GetValueAsString(nsAString& aValue) const
28 : {
29 0 : aValue.Truncate();
30 : char16_t buf[24];
31 0 : uint32_t last = mNumbers.Length() - 1;
32 0 : for (uint32_t i = 0; i < mNumbers.Length(); ++i) {
33 : // Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not
34 : // possible to always avoid trailing zeros.
35 0 : nsTextFormatter::snprintf(buf, ArrayLength(buf),
36 : u"%g",
37 0 : double(mNumbers[i]));
38 : // We ignore OOM, since it's not useful for us to return an error.
39 0 : aValue.Append(buf);
40 0 : if (i != last) {
41 0 : aValue.Append(' ');
42 : }
43 : }
44 0 : }
45 :
46 : nsresult
47 0 : SVGNumberList::SetValueFromString(const nsAString& aValue)
48 : {
49 0 : SVGNumberList temp;
50 :
51 : nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
52 0 : tokenizer(aValue, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
53 :
54 0 : while (tokenizer.hasMoreTokens()) {
55 : float num;
56 0 : if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), num)) {
57 0 : return NS_ERROR_DOM_SYNTAX_ERR;
58 : }
59 0 : if (!temp.AppendItem(num)) {
60 0 : return NS_ERROR_OUT_OF_MEMORY;
61 : }
62 : }
63 0 : if (tokenizer.separatorAfterCurrentToken()) {
64 0 : return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
65 : }
66 0 : return CopyFrom(temp);
67 : }
68 :
69 : } // namespace mozilla
|