tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGNumberList.cpp (1952B)


      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 "SVGNumberList.h"
      8 
      9 #include "SVGContentUtils.h"
     10 #include "nsCharSeparatedTokenizer.h"
     11 #include "nsContentUtils.h"
     12 #include "nsString.h"
     13 #include "nsTextFormatter.h"
     14 
     15 namespace mozilla {
     16 
     17 nsresult SVGNumberList::CopyFrom(const SVGNumberList& rhs) {
     18  if (!mNumbers.Assign(rhs.mNumbers, fallible)) {
     19    return NS_ERROR_OUT_OF_MEMORY;
     20  }
     21  return NS_OK;
     22 }
     23 
     24 void SVGNumberList::GetValueAsString(nsAString& aValue) const {
     25  aValue.Truncate();
     26  char16_t buf[24];
     27  uint32_t last = mNumbers.Length() - 1;
     28  for (uint32_t i = 0; i < mNumbers.Length(); ++i) {
     29    // Would like to use aValue.AppendPrintf("%f", mNumbers[i]), but it's not
     30    // possible to always avoid trailing zeros.
     31    nsTextFormatter::snprintf(buf, std::size(buf), u"%g", double(mNumbers[i]));
     32    // We ignore OOM, since it's not useful for us to return an error.
     33    aValue.Append(buf);
     34    if (i != last) {
     35      aValue.Append(' ');
     36    }
     37  }
     38 }
     39 
     40 nsresult SVGNumberList::SetValueFromString(const nsAString& aValue) {
     41  SVGNumberList temp;
     42 
     43  nsCharSeparatedTokenizerTemplate<nsContentUtils::IsHTMLWhitespace,
     44                                   nsTokenizerFlags::SeparatorOptional>
     45      tokenizer(aValue, ',');
     46 
     47  while (tokenizer.hasMoreTokens()) {
     48    float num;
     49    if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), num)) {
     50      return NS_ERROR_DOM_SYNTAX_ERR;
     51    }
     52    if (!temp.AppendItem(num)) {
     53      return NS_ERROR_OUT_OF_MEMORY;
     54    }
     55  }
     56  if (tokenizer.separatorAfterCurrentToken()) {
     57    return NS_ERROR_DOM_SYNTAX_ERR;  // trailing comma
     58  }
     59  mNumbers = std::move(temp.mNumbers);
     60  return NS_OK;
     61 }
     62 
     63 }  // namespace mozilla