parser/html/nsHtml5Portability.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/nsHtml5Portability.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsIAtom.h"
     1.9 +#include "nsString.h"
    1.10 +#include "jArray.h"
    1.11 +#include "nsHtml5Portability.h"
    1.12 +
    1.13 +nsIAtom*
    1.14 +nsHtml5Portability::newLocalNameFromBuffer(char16_t* buf, int32_t offset, int32_t length, nsHtml5AtomTable* interner)
    1.15 +{
    1.16 +  NS_ASSERTION(!offset, "The offset should always be zero here.");
    1.17 +  NS_ASSERTION(interner, "Didn't get an atom service.");
    1.18 +  return interner->GetAtom(nsDependentSubstring(buf, buf + length));
    1.19 +}
    1.20 +
    1.21 +nsString*
    1.22 +nsHtml5Portability::newStringFromBuffer(char16_t* buf, int32_t offset, int32_t length)
    1.23 +{
    1.24 +  return new nsString(buf + offset, length);
    1.25 +}
    1.26 +
    1.27 +nsString*
    1.28 +nsHtml5Portability::newEmptyString()
    1.29 +{
    1.30 +  return new nsString();
    1.31 +}
    1.32 +
    1.33 +nsString*
    1.34 +nsHtml5Portability::newStringFromLiteral(const char* literal)
    1.35 +{
    1.36 +  nsString* str = new nsString();
    1.37 +  str->AssignASCII(literal);
    1.38 +  return str;
    1.39 +}
    1.40 +
    1.41 +nsString*
    1.42 +nsHtml5Portability::newStringFromString(nsString* string) {
    1.43 +  nsString* newStr = new nsString();
    1.44 +  newStr->Assign(*string);
    1.45 +  return newStr;
    1.46 +}
    1.47 +
    1.48 +jArray<char16_t,int32_t>
    1.49 +nsHtml5Portability::newCharArrayFromLocal(nsIAtom* local)
    1.50 +{
    1.51 +  nsAutoString temp;
    1.52 +  local->ToString(temp);
    1.53 +  int32_t len = temp.Length();
    1.54 +  jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
    1.55 +  memcpy(arr, temp.BeginReading(), len * sizeof(char16_t));
    1.56 +  return arr;
    1.57 +}
    1.58 +
    1.59 +jArray<char16_t,int32_t>
    1.60 +nsHtml5Portability::newCharArrayFromString(nsString* string)
    1.61 +{
    1.62 +  int32_t len = string->Length();
    1.63 +  jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
    1.64 +  memcpy(arr, string->BeginReading(), len * sizeof(char16_t));
    1.65 +  return arr;
    1.66 +}
    1.67 +
    1.68 +nsIAtom*
    1.69 +nsHtml5Portability::newLocalFromLocal(nsIAtom* local, nsHtml5AtomTable* interner)
    1.70 +{
    1.71 +  NS_PRECONDITION(local, "Atom was null.");
    1.72 +  NS_PRECONDITION(interner, "Atom table was null");
    1.73 +  if (!local->IsStaticAtom()) {
    1.74 +    nsAutoString str;
    1.75 +    local->ToString(str);
    1.76 +    local = interner->GetAtom(str);
    1.77 +  }
    1.78 +  return local;
    1.79 +}
    1.80 +
    1.81 +void
    1.82 +nsHtml5Portability::releaseString(nsString* str)
    1.83 +{
    1.84 +  delete str;
    1.85 +}
    1.86 +
    1.87 +bool
    1.88 +nsHtml5Portability::localEqualsBuffer(nsIAtom* local, char16_t* buf, int32_t offset, int32_t length)
    1.89 +{
    1.90 +  return local->Equals(nsDependentSubstring(buf + offset, buf + offset + length));
    1.91 +}
    1.92 +
    1.93 +bool
    1.94 +nsHtml5Portability::lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(const char* lowerCaseLiteral, nsString* string)
    1.95 +{
    1.96 +  if (!string) {
    1.97 +    return false;
    1.98 +  }
    1.99 +  const char* litPtr = lowerCaseLiteral;
   1.100 +  const char16_t* strPtr = string->BeginReading();
   1.101 +  const char16_t* end = string->EndReading();
   1.102 +  char16_t litChar;
   1.103 +  while ((litChar = *litPtr)) {
   1.104 +    NS_ASSERTION(!(litChar >= 'A' && litChar <= 'Z'), "Literal isn't in lower case.");
   1.105 +    if (strPtr == end) {
   1.106 +      return false;
   1.107 +    }
   1.108 +    char16_t strChar = *strPtr;
   1.109 +    if (strChar >= 'A' && strChar <= 'Z') {
   1.110 +      strChar += 0x20;
   1.111 +    }
   1.112 +    if (litChar != strChar) {
   1.113 +      return false;
   1.114 +    }
   1.115 +    ++litPtr;
   1.116 +    ++strPtr;
   1.117 +  }
   1.118 +  return true;
   1.119 +}
   1.120 +
   1.121 +bool
   1.122 +nsHtml5Portability::lowerCaseLiteralEqualsIgnoreAsciiCaseString(const char* lowerCaseLiteral, nsString* string)
   1.123 +{
   1.124 +  if (!string) {
   1.125 +    return false;
   1.126 +  }
   1.127 +  return string->LowerCaseEqualsASCII(lowerCaseLiteral);
   1.128 +}
   1.129 +
   1.130 +bool
   1.131 +nsHtml5Portability::literalEqualsString(const char* literal, nsString* string)
   1.132 +{
   1.133 +  if (!string) {
   1.134 +    return false;
   1.135 +  }
   1.136 +  return string->EqualsASCII(literal);
   1.137 +}
   1.138 +
   1.139 +bool
   1.140 +nsHtml5Portability::stringEqualsString(nsString* one, nsString* other)
   1.141 +{
   1.142 +  return one->Equals(*other);
   1.143 +}
   1.144 +
   1.145 +void
   1.146 +nsHtml5Portability::initializeStatics()
   1.147 +{
   1.148 +}
   1.149 +
   1.150 +void
   1.151 +nsHtml5Portability::releaseStatics()
   1.152 +{
   1.153 +}

mercurial