content/html/content/src/nsMediaFragmentURIParser.cpp
author Ehsan Akhgari <ehsan@mozilla.com>
Wed, 22 Aug 2012 11:56:38 -0400
changeset 108991 a16372ce30b5f6b747246b01fcd215a4bf3b6342
parent 106878 6e30103582c44ea1ee8c25747bd3bed6671c0011
child 110974 3d9424eb6eb473cfd14309fd635ff7d6e29322c2
child 113574 c4f83d9d8243f3f853a5356188164a5fddee2b5a
permissions -rw-r--r--
Bug 579517 - Part 1: Automated conversion of NSPR numeric types to stdint types in Gecko; r=bsmedberg This patch was generated by a script. Here's the source of the script for future reference: function convert() { echo "Converting $1 to $2..." find . ! -wholename "*nsprpub*" \ ! -wholename "*security/nss*" \ ! -wholename "*/.hg*" \ ! -wholename "obj-ff-dbg*" \ ! -name nsXPCOMCID.h \ ! -name prtypes.h \ -type f \ \( -iname "*.cpp" \ -o -iname "*.h" \ -o -iname "*.c" \ -o -iname "*.cc" \ -o -iname "*.idl" \ -o -iname "*.ipdl" \ -o -iname "*.ipdlh" \ -o -iname "*.mm" \) | \ xargs -n 1 sed -i -e "s/\b$1\b/$2/g" } convert PRInt8 int8_t convert PRUint8 uint8_t convert PRInt16 int16_t convert PRUint16 uint16_t convert PRInt32 int32_t convert PRUint32 uint32_t convert PRInt64 int64_t convert PRUint64 uint64_t convert PRIntn int convert PRUintn unsigned convert PRSize size_t convert PROffset32 int32_t convert PROffset64 int64_t convert PRPtrdiff ptrdiff_t convert PRFloat64 double

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCharSeparatedTokenizer.h"
#include "nsEscape.h"
#include "nsMediaFragmentURIParser.h"

nsMediaFragmentURIParser::nsMediaFragmentURIParser(const nsCString& aSpec)
{
  nsReadingIterator<char> start, end;
  aSpec.BeginReading(start);
  aSpec.EndReading(end);
  if (FindCharInReadable('#', start, end)) {
    mHash = Substring(++start, end);
  }
}

bool nsMediaFragmentURIParser::ParseNPT(nsDependentSubstring& aString, double& aStart, double& aEnd)
{
  nsDependentSubstring original(aString);
  if (aString.Length() > 4 &&
      aString[0] == 'n' && aString[1] == 'p' &&
      aString[2] == 't' && aString[3] == ':') {
    aString.Rebind(aString, 4);
  }

  if (aString.Length() == 0) {
    return false;
  }

  double start = -1.0;
  double end = -1.0;

  if (ParseNPTTime(aString, start)) {
    aStart = start;
  }

  if (aString.Length() == 0) {
    return true;
  }

  if (aString[0] != ',') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);

  if (aString.Length() == 0) {
    aString.Rebind(original, 0);
    return false;
  }

  if (ParseNPTTime(aString, end)) {
    aEnd = end;
  }

  if (aString.Length() != 0) {
    aString.Rebind(original, 0);
    return false;
  }

  return true;
}

bool nsMediaFragmentURIParser::ParseNPTTime(nsDependentSubstring& aString, double& aTime)
{
  if (aString.Length() == 0) {
    return false;
  }

  return
    ParseNPTHHMMSS(aString, aTime) ||
    ParseNPTMMSS(aString, aTime) ||
    ParseNPTSec(aString, aTime);
}

// Return true if the given character is a numeric character
static bool IsDigit(nsDependentSubstring::char_type aChar)
{
  return (aChar >= '0' && aChar <= '9');
}

// Return the index of the first character in the string that is not
// a numerical digit, starting from 'aStart'.
static uint32_t FirstNonDigit(nsDependentSubstring& aString, uint32_t aStart)
{
   while (aStart < aString.Length() && IsDigit(aString[aStart])) {
    ++aStart;
  }
  return aStart;
}
 
bool nsMediaFragmentURIParser::ParseNPTSec(nsDependentSubstring& aString, double& aSec)
{
  nsDependentSubstring original(aString);
  if (aString.Length() == 0) {
    return false;
  }

  uint32_t index = FirstNonDigit(aString, 0);
  if (index == 0) {
    return false;
  }

  nsDependentSubstring n(aString, 0, index);
  nsresult ec;
  int32_t s = PromiseFlatString(n).ToInteger(&ec);
  if (NS_FAILED(ec)) {
    return false;
  }

  aString.Rebind(aString, index);
  double fraction = 0.0;
  if (!ParseNPTFraction(aString, fraction)) {
    aString.Rebind(original, 0);
    return false;
  }

  aSec = s + fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTMMSS(nsDependentSubstring& aString, double& aTime)
{
  nsDependentSubstring original(aString);
  uint32_t mm = 0;
  uint32_t ss = 0;
  double fraction = 0.0;
  if (!ParseNPTMM(aString, mm)) {
    aString.Rebind(original, 0);
    return false;
  }

  if (aString.Length() < 2 || aString[0] != ':') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);
  if (!ParseNPTSS(aString, ss)) {
    aString.Rebind(original, 0);
    return false;
  }

  if (!ParseNPTFraction(aString, fraction)) {
    aString.Rebind(original, 0);
    return false;
  }
  aTime = mm * 60 + ss + fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTFraction(nsDependentSubstring& aString, double& aFraction)
{
  double fraction = 0.0;

  if (aString.Length() > 0 && aString[0] == '.') {
    uint32_t index = FirstNonDigit(aString, 1);

    if (index > 1) {
      nsDependentSubstring number(aString, 0, index);
      nsresult ec;
      fraction = PromiseFlatString(number).ToDouble(&ec);
      if (NS_FAILED(ec)) {
        return false;
      }
    }
    aString.Rebind(aString, index);
  }

  aFraction = fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime)
{
  nsDependentSubstring original(aString);
  uint32_t hh = 0;
  double seconds = 0.0;
  if (!ParseNPTHH(aString, hh)) {
    return false;
  }

  if (aString.Length() < 2 || aString[0] != ':') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);
  if (!ParseNPTMMSS(aString, seconds)) {
    aString.Rebind(original, 0);
    return false;
  }

  aTime = hh * 3600 + seconds;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour)
{
  if (aString.Length() == 0) {
    return false;
  }

  uint32_t index = FirstNonDigit(aString, 0);
  if (index == 0) {
    return false;
  }

  nsDependentSubstring n(aString, 0, index);
  nsresult ec;
  int32_t u = PromiseFlatString(n).ToInteger(&ec);
  if (NS_FAILED(ec)) {
    return false;
  }

  aString.Rebind(aString, index);
  aHour = u;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute)
{
  return ParseNPTSS(aString, aMinute);
}

bool nsMediaFragmentURIParser::ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond)
{
  if (aString.Length() < 2) {
    return false;
  }

  if (IsDigit(aString[0]) && IsDigit(aString[1])) {
    nsDependentSubstring n(aString, 0, 2);
    nsresult ec;
    int32_t u = PromiseFlatString(n).ToInteger(&ec);
    if (NS_FAILED(ec)) {
      return false;
    }

    aString.Rebind(aString, 2);
    if (u >= 60)
      return false;

    aSecond = u;
    return true;
  }

  return false;
}

void nsMediaFragmentURIParser::Parse()
{
  nsCCharSeparatedTokenizer tokenizer(mHash, '&');
  while (tokenizer.hasMoreTokens()) {
    const nsCSubstring& nv = tokenizer.nextToken();
    int32_t index = nv.FindChar('=');
    if (index >= 0) {
      nsCAutoString name;
      nsCAutoString value;
      NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name);
      NS_UnescapeURL(Substring(nv, index + 1, nv.Length()),
                     esc_Ref | esc_AlwaysCopy, value);
      nsAutoString a = NS_ConvertUTF8toUTF16(name);
      nsAutoString b = NS_ConvertUTF8toUTF16(value);
      mFragments.AppendElement(Pair(a, b));
    }
  }
}

double nsMediaFragmentURIParser::GetStartTime()
{
  for (uint32_t i = 0; i < mFragments.Length(); ++i) {
    uint32_t index = mFragments.Length() - i - 1;
    if (mFragments[index].mName.EqualsLiteral("t")) {
      double start = -1;
      double end = -1;
      nsDependentSubstring s(mFragments[index].mValue, 0);
      if (ParseNPT(s, start, end)) {
        return start;
      }
    }
  }
  return 0.0;
}

double nsMediaFragmentURIParser::GetEndTime()
{
  for (uint32_t i = 0; i < mFragments.Length(); ++i) {
    uint32_t index = mFragments.Length() - i - 1;
    if (mFragments[index].mName.EqualsLiteral("t")) {
      double start = -1;
      double end = -1;
      nsDependentSubstring s(mFragments[index].mValue, 0);
      if (ParseNPT(s, start, end)) {
        return end;
      }
    }
  }
  return -1;
}