/src/mozilla-central/dom/svg/SVGNumberList.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | SVGNumberList::CopyFrom(const SVGNumberList& rhs) |
19 | 0 | { |
20 | 0 | if (!mNumbers.Assign(rhs.mNumbers, fallible)) { |
21 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
22 | 0 | } |
23 | 0 | return NS_OK; |
24 | 0 | } |
25 | | |
26 | | void |
27 | | SVGNumberList::GetValueAsString(nsAString& aValue) const |
28 | 0 | { |
29 | 0 | aValue.Truncate(); |
30 | 0 | char16_t buf[24]; |
31 | 0 | uint32_t last = mNumbers.Length() - 1; |
32 | 0 | for (uint32_t i = 0; i < mNumbers.Length(); ++i) { |
33 | 0 | // Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not |
34 | 0 | // possible to always avoid trailing zeros. |
35 | 0 | nsTextFormatter::snprintf(buf, ArrayLength(buf), |
36 | 0 | u"%g", |
37 | 0 | double(mNumbers[i])); |
38 | 0 | // 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 | 0 | } |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | nsresult |
47 | | SVGNumberList::SetValueFromString(const nsAString& aValue) |
48 | 0 | { |
49 | 0 | SVGNumberList temp; |
50 | 0 |
|
51 | 0 | nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace> |
52 | 0 | tokenizer(aValue, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL); |
53 | 0 |
|
54 | 0 | while (tokenizer.hasMoreTokens()) { |
55 | 0 | float num; |
56 | 0 | if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), num)) { |
57 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; |
58 | 0 | } |
59 | 0 | if (!temp.AppendItem(num)) { |
60 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
61 | 0 | } |
62 | 0 | } |
63 | 0 | if (tokenizer.separatorAfterCurrentToken()) { |
64 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma |
65 | 0 | } |
66 | 0 | return CopyFrom(temp); |
67 | 0 | } |
68 | | |
69 | | } // namespace mozilla |