Bug 1700051: part 46) Const-qualify `mozInlineSpellStatus::mAnchorRange`. r=smaug
[gecko.git] / dom / svg / SVGNumberList.cpp
blobd41859d9479d91bc685b2c24225e2a4fa422da74
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/. */
7 #include "SVGNumberList.h"
9 #include "mozilla/ArrayUtils.h"
10 #include "nsCharSeparatedTokenizer.h"
11 #include "nsContentUtils.h"
12 #include "nsString.h"
13 #include "nsTextFormatter.h"
14 #include "SVGContentUtils.h"
16 namespace mozilla {
18 nsresult SVGNumberList::CopyFrom(const SVGNumberList& rhs) {
19 if (!mNumbers.Assign(rhs.mNumbers, fallible)) {
20 return NS_ERROR_OUT_OF_MEMORY;
22 return NS_OK;
25 void SVGNumberList::GetValueAsString(nsAString& aValue) const {
26 aValue.Truncate();
27 char16_t buf[24];
28 uint32_t last = mNumbers.Length() - 1;
29 for (uint32_t i = 0; i < mNumbers.Length(); ++i) {
30 // Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not
31 // possible to always avoid trailing zeros.
32 nsTextFormatter::snprintf(buf, ArrayLength(buf), u"%g",
33 double(mNumbers[i]));
34 // We ignore OOM, since it's not useful for us to return an error.
35 aValue.Append(buf);
36 if (i != last) {
37 aValue.Append(' ');
42 nsresult SVGNumberList::SetValueFromString(const nsAString& aValue) {
43 SVGNumberList temp;
45 nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace,
46 nsTokenizerFlags::SeparatorOptional>
47 tokenizer(aValue, ',');
49 while (tokenizer.hasMoreTokens()) {
50 float num;
51 if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), num)) {
52 return NS_ERROR_DOM_SYNTAX_ERR;
54 if (!temp.AppendItem(num)) {
55 return NS_ERROR_OUT_OF_MEMORY;
58 if (tokenizer.separatorAfterCurrentToken()) {
59 return NS_ERROR_DOM_SYNTAX_ERR; // trailing comma
61 return CopyFrom(temp);
64 } // namespace mozilla