2010-04-02 07:03:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2010-04-27 12:53:44 +04:00
|
|
|
#ifndef VideoUtils_h
|
|
|
|
#define VideoUtils_h
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-05-14 23:50:20 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2014-06-11 02:53:21 +04:00
|
|
|
#include "nsIThread.h"
|
2015-03-29 17:59:08 +03:00
|
|
|
#include "nsSize.h"
|
2015-04-21 18:04:57 +03:00
|
|
|
#include "nsRect.h"
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-06-10 19:30:00 +04:00
|
|
|
#include "prtime.h"
|
2013-09-26 23:06:59 +04:00
|
|
|
#include "AudioSampleFormat.h"
|
2015-05-18 09:15:47 +03:00
|
|
|
#include "TimeUnits.h"
|
2015-09-29 03:06:11 +03:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2011-06-24 02:08:54 +04:00
|
|
|
|
2012-02-22 16:28:06 +04:00
|
|
|
using mozilla::CheckedInt64;
|
|
|
|
using mozilla::CheckedUint64;
|
|
|
|
using mozilla::CheckedInt32;
|
|
|
|
using mozilla::CheckedUint32;
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
// This file contains stuff we'd rather put elsewhere, but which is
|
|
|
|
// dependent on other changes which we don't want to wait for. We plan to
|
|
|
|
// remove this file in the near future.
|
|
|
|
|
|
|
|
|
|
|
|
// This belongs in xpcom/monitor/Monitor.h, once we've made
|
|
|
|
// mozilla::Monitor non-reentrant.
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
/**
|
|
|
|
* ReentrantMonitorConditionallyEnter
|
|
|
|
*
|
|
|
|
* Enters the supplied monitor only if the conditional value |aEnter| is true.
|
|
|
|
* E.g. Used to allow unmonitored read access on the decode thread,
|
|
|
|
* and monitored access on all other threads.
|
|
|
|
*/
|
2013-04-12 07:20:09 +04:00
|
|
|
class MOZ_STACK_CLASS ReentrantMonitorConditionallyEnter
|
2012-12-07 03:27:08 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReentrantMonitorConditionallyEnter(bool aEnter,
|
|
|
|
ReentrantMonitor &aReentrantMonitor) :
|
|
|
|
mReentrantMonitor(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ReentrantMonitorConditionallyEnter);
|
|
|
|
if (aEnter) {
|
|
|
|
mReentrantMonitor = &aReentrantMonitor;
|
|
|
|
NS_ASSERTION(mReentrantMonitor, "null monitor");
|
|
|
|
mReentrantMonitor->Enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~ReentrantMonitorConditionallyEnter(void)
|
|
|
|
{
|
|
|
|
if (mReentrantMonitor) {
|
|
|
|
mReentrantMonitor->Exit();
|
|
|
|
}
|
|
|
|
MOZ_COUNT_DTOR(ReentrantMonitorConditionallyEnter);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// Restrict to constructor and destructor defined above.
|
|
|
|
ReentrantMonitorConditionallyEnter();
|
|
|
|
ReentrantMonitorConditionallyEnter(const ReentrantMonitorConditionallyEnter&);
|
|
|
|
ReentrantMonitorConditionallyEnter& operator =(const ReentrantMonitorConditionallyEnter&);
|
|
|
|
static void* operator new(size_t) CPP_THROW_NEW;
|
|
|
|
static void operator delete(void*);
|
|
|
|
|
|
|
|
ReentrantMonitor* mReentrantMonitor;
|
|
|
|
};
|
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// Shuts down a thread asynchronously.
|
2016-04-26 03:23:21 +03:00
|
|
|
class ShutdownThreadEvent : public Runnable
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
|
2012-03-28 04:04:20 +04:00
|
|
|
~ShutdownThreadEvent() {}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2012-03-28 04:04:20 +04:00
|
|
|
mThread->Shutdown();
|
2012-07-30 18:20:58 +04:00
|
|
|
mThread = nullptr;
|
2012-03-28 04:04:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2014-06-11 02:53:21 +04:00
|
|
|
template<class T>
|
2016-04-26 03:23:21 +03:00
|
|
|
class DeleteObjectTask: public Runnable {
|
2014-06-11 02:53:21 +04:00
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit DeleteObjectTask(nsAutoPtr<T>& aObject)
|
2014-06-11 02:53:21 +04:00
|
|
|
: mObject(aObject)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be on main thread.");
|
|
|
|
mObject = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsAutoPtr<T> mObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void DeleteOnMainThread(nsAutoPtr<T>& aObject) {
|
|
|
|
NS_DispatchToMainThread(new DeleteObjectTask<T>(aObject));
|
|
|
|
}
|
|
|
|
|
2012-09-03 02:56:29 +04:00
|
|
|
class MediaResource;
|
2010-08-13 06:28:15 +04:00
|
|
|
|
2012-09-03 02:56:29 +04:00
|
|
|
// Estimates the buffered ranges of a MediaResource using a simple
|
|
|
|
// (byteOffset/length)*duration method. Probably inaccurate, but won't
|
|
|
|
// do file I/O, and can be used when we don't have detailed knowledge
|
|
|
|
// of the byte->time mapping of a resource. aDurationUsecs is the duration
|
|
|
|
// of the media in microseconds. Estimated buffered ranges are stored in
|
|
|
|
// aOutBuffered. Ranges are 0-normalized, i.e. in the range of (0,duration].
|
2015-05-18 09:15:47 +03:00
|
|
|
media::TimeIntervals GetEstimatedBufferedTimeRanges(mozilla::MediaResource* aStream,
|
|
|
|
int64_t aDurationUsecs);
|
2012-09-03 02:56:29 +04:00
|
|
|
|
2011-09-27 07:31:18 +04:00
|
|
|
// Converts from number of audio frames (aFrames) to microseconds, given
|
2015-07-01 09:50:27 +03:00
|
|
|
// the specified audio rate (aRate).
|
2012-08-22 19:56:38 +04:00
|
|
|
CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate);
|
2015-07-01 09:50:27 +03:00
|
|
|
// Converts from number of audio frames (aFrames) TimeUnit, given
|
|
|
|
// the specified audio rate (aRate).
|
|
|
|
media::TimeUnit FramesToTimeUnit(int64_t aFrames, uint32_t aRate);
|
2016-04-26 04:16:23 +03:00
|
|
|
// Perform aValue * aMul / aDiv, reducing the possibility of overflow due to
|
|
|
|
// aValue * aMul overflowing.
|
|
|
|
CheckedInt64 SaferMultDiv(int64_t aValue, uint32_t aMul, uint32_t aDiv);
|
2010-08-13 06:28:15 +04:00
|
|
|
|
2011-09-27 07:31:18 +04:00
|
|
|
// Converts from microseconds (aUsecs) to number of audio frames, given the
|
|
|
|
// specified audio rate (aRate). Stores the result in aOutFrames. Returns
|
2011-09-30 03:34:37 +04:00
|
|
|
// true if the operation succeeded, or false if there was an integer
|
2010-08-13 06:28:15 +04:00
|
|
|
// overflow while calulating the conversion.
|
2012-08-22 19:56:38 +04:00
|
|
|
CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate);
|
2011-04-14 02:12:23 +04:00
|
|
|
|
2015-06-18 17:45:05 +03:00
|
|
|
// Format TimeUnit as number of frames at given rate.
|
|
|
|
CheckedInt64 TimeUnitToFrames(const media::TimeUnit& aTime, uint32_t aRate);
|
|
|
|
|
2015-02-24 06:11:43 +03:00
|
|
|
// Converts milliseconds to seconds.
|
|
|
|
#define MS_TO_SECONDS(ms) ((double)(ms) / (PR_MSEC_PER_SEC))
|
|
|
|
|
2013-06-10 19:30:00 +04:00
|
|
|
// Converts seconds to milliseconds.
|
2015-02-24 06:11:43 +03:00
|
|
|
#define SECONDS_TO_MS(s) ((int)((s) * (PR_MSEC_PER_SEC)))
|
2013-06-10 19:30:00 +04:00
|
|
|
|
2014-04-01 07:39:04 +04:00
|
|
|
// Converts from seconds to microseconds. Returns failure if the resulting
|
|
|
|
// integer is too big to fit in an int64_t.
|
|
|
|
nsresult SecondsToUsecs(double aSeconds, int64_t& aOutUsecs);
|
|
|
|
|
2011-06-24 02:08:54 +04:00
|
|
|
// The maximum height and width of the video. Used for
|
|
|
|
// sanitizing the memory allocation of the RGB buffer.
|
|
|
|
// The maximum resolution we anticipate encountering in the
|
2015-11-06 00:45:00 +03:00
|
|
|
// wild is 2160p (UHD "4K") or 4320p - 7680x4320 pixels for VR.
|
|
|
|
static const int32_t MAX_VIDEO_WIDTH = 8192;
|
|
|
|
static const int32_t MAX_VIDEO_HEIGHT = 4608;
|
2011-06-24 02:08:54 +04:00
|
|
|
|
|
|
|
// Scales the display rect aDisplay by aspect ratio aAspectRatio.
|
2014-02-04 05:49:21 +04:00
|
|
|
// Note that aDisplay must be validated by IsValidVideoRegion()
|
2011-06-24 02:08:54 +04:00
|
|
|
// before being used!
|
|
|
|
void ScaleDisplayByAspectRatio(nsIntSize& aDisplay, float aAspectRatio);
|
|
|
|
|
2015-08-06 09:30:22 +03:00
|
|
|
// Downmix Stereo audio samples to Mono.
|
|
|
|
// Input are the buffer contains stereo data and the number of frames.
|
|
|
|
void DownmixStereoToMono(mozilla::AudioDataValue* aBuffer,
|
|
|
|
uint32_t aFrames);
|
|
|
|
|
2013-11-04 02:45:19 +04:00
|
|
|
bool IsVideoContentType(const nsCString& aContentType);
|
|
|
|
|
2014-02-04 05:49:21 +04:00
|
|
|
// Returns true if it's safe to use aPicture as the picture to be
|
|
|
|
// extracted inside a frame of size aFrame, and scaled up to and displayed
|
|
|
|
// at a size of aDisplay. You should validate the frame, picture, and
|
|
|
|
// display regions before using them to display video frames.
|
|
|
|
bool IsValidVideoRegion(const nsIntSize& aFrame, const nsIntRect& aPicture,
|
|
|
|
const nsIntSize& aDisplay);
|
|
|
|
|
2014-05-06 04:12:05 +04:00
|
|
|
// Template to automatically set a variable to a value on scope exit.
|
|
|
|
// Useful for unsetting flags, etc.
|
|
|
|
template<typename T>
|
|
|
|
class AutoSetOnScopeExit {
|
|
|
|
public:
|
|
|
|
AutoSetOnScopeExit(T& aVar, T aValue)
|
|
|
|
: mVar(aVar)
|
|
|
|
, mValue(aValue)
|
|
|
|
{}
|
|
|
|
~AutoSetOnScopeExit() {
|
|
|
|
mVar = mValue;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T& mVar;
|
|
|
|
const T mValue;
|
|
|
|
};
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
class SharedThreadPool;
|
|
|
|
|
2015-05-07 07:01:43 +03:00
|
|
|
// The MediaDataDecoder API blocks, with implementations waiting on platform
|
|
|
|
// decoder tasks. These platform decoder tasks are queued on a separate
|
|
|
|
// thread pool to ensure they can run when the MediaDataDecoder clients'
|
|
|
|
// thread pool is blocked. Tasks on the PLATFORM_DECODER thread pool must not
|
|
|
|
// wait on tasks in the PLAYBACK thread pool.
|
|
|
|
//
|
|
|
|
// No new dependencies on this mechanism should be added, as methods are being
|
2015-07-16 21:06:49 +03:00
|
|
|
// made async supported by MozPromise, making this unnecessary and
|
2015-05-07 07:01:43 +03:00
|
|
|
// permitting unifying the pool.
|
|
|
|
enum class MediaThreadType {
|
|
|
|
PLAYBACK, // MediaDecoderStateMachine and MediaDecoderReader
|
|
|
|
PLATFORM_DECODER
|
|
|
|
};
|
2014-06-18 09:07:02 +04:00
|
|
|
// Returns the thread pool that is shared amongst all decoder state machines
|
|
|
|
// for decoding streams.
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SharedThreadPool> GetMediaThreadPool(MediaThreadType aType);
|
2014-06-18 09:07:02 +04:00
|
|
|
|
2014-08-28 07:32:34 +04:00
|
|
|
enum H264_PROFILE {
|
|
|
|
H264_PROFILE_UNKNOWN = 0,
|
|
|
|
H264_PROFILE_BASE = 0x42,
|
|
|
|
H264_PROFILE_MAIN = 0x4D,
|
|
|
|
H264_PROFILE_EXTENDED = 0x58,
|
|
|
|
H264_PROFILE_HIGH = 0x64,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum H264_LEVEL {
|
|
|
|
H264_LEVEL_1 = 10,
|
|
|
|
H264_LEVEL_1_b = 11,
|
|
|
|
H264_LEVEL_1_1 = 11,
|
|
|
|
H264_LEVEL_1_2 = 12,
|
|
|
|
H264_LEVEL_1_3 = 13,
|
|
|
|
H264_LEVEL_2 = 20,
|
|
|
|
H264_LEVEL_2_1 = 21,
|
|
|
|
H264_LEVEL_2_2 = 22,
|
|
|
|
H264_LEVEL_3 = 30,
|
|
|
|
H264_LEVEL_3_1 = 31,
|
|
|
|
H264_LEVEL_3_2 = 32,
|
|
|
|
H264_LEVEL_4 = 40,
|
|
|
|
H264_LEVEL_4_1 = 41,
|
|
|
|
H264_LEVEL_4_2 = 42,
|
|
|
|
H264_LEVEL_5 = 50,
|
|
|
|
H264_LEVEL_5_1 = 51,
|
|
|
|
H264_LEVEL_5_2 = 52
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extracts the H.264/AVC profile and level from an H.264 codecs string.
|
|
|
|
// H.264 codecs parameters have a type defined as avc1.PPCCLL, where
|
|
|
|
// PP = profile_idc, CC = constraint_set flags, LL = level_idc.
|
|
|
|
// See http://blog.pearce.org.nz/2013/11/what-does-h264avc1-codecs-parameters.html
|
|
|
|
// for more details.
|
|
|
|
// Returns false on failure.
|
|
|
|
bool
|
|
|
|
ExtractH264CodecDetails(const nsAString& aCodecs,
|
|
|
|
int16_t& aProfile,
|
|
|
|
int16_t& aLevel);
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
// Use a cryptographic quality PRNG to generate raw random bytes
|
2015-03-03 17:51:05 +03:00
|
|
|
// and convert that to a base64 string.
|
|
|
|
nsresult
|
|
|
|
GenerateRandomName(nsCString& aOutSalt, uint32_t aLength);
|
|
|
|
|
|
|
|
// This version returns a string suitable for use as a file or URL
|
2014-10-13 02:53:43 +04:00
|
|
|
// path. This is based on code from nsExternalAppHandler::SetUpTempFile.
|
|
|
|
nsresult
|
|
|
|
GenerateRandomPathName(nsCString& aOutSalt, uint32_t aLength);
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
already_AddRefed<TaskQueue>
|
2014-12-09 22:34:00 +03:00
|
|
|
CreateMediaDecodeTaskQueue();
|
|
|
|
|
2015-06-30 02:49:47 +03:00
|
|
|
// Iteratively invokes aWork until aCondition returns true, or aWork returns false.
|
|
|
|
// Use this rather than a while loop to avoid bogarting the task queue.
|
|
|
|
template<class Work, class Condition>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GenericPromise> InvokeUntil(Work aWork, Condition aCondition) {
|
|
|
|
RefPtr<GenericPromise::Private> p = new GenericPromise::Private(__func__);
|
2015-06-30 02:49:47 +03:00
|
|
|
|
|
|
|
if (aCondition()) {
|
|
|
|
p->Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Helper {
|
2015-10-18 08:24:48 +03:00
|
|
|
static void Iteration(RefPtr<GenericPromise::Private> aPromise, Work aLocalWork, Condition aLocalCondition) {
|
2015-09-30 08:32:32 +03:00
|
|
|
if (!aLocalWork()) {
|
2015-06-30 02:49:47 +03:00
|
|
|
aPromise->Reject(NS_ERROR_FAILURE, __func__);
|
2015-09-30 08:32:32 +03:00
|
|
|
} else if (aLocalCondition()) {
|
2015-06-30 02:49:47 +03:00
|
|
|
aPromise->Resolve(true, __func__);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
2015-09-30 08:32:32 +03:00
|
|
|
NS_NewRunnableFunction([aPromise, aLocalWork, aLocalCondition] () { Iteration(aPromise, aLocalWork, aLocalCondition); });
|
2015-06-30 02:49:47 +03:00
|
|
|
AbstractThread::GetCurrent()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Helper::Iteration(p, aWork, aCondition);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:06:11 +03:00
|
|
|
// Simple timer to run a runnable after a timeout.
|
|
|
|
class SimpleTimer : public nsITimerCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// Create a new timer to run aTask after aTimeoutMs milliseconds
|
|
|
|
// on thread aTarget. If aTarget is null, task is run on the main thread.
|
|
|
|
static already_AddRefed<SimpleTimer> Create(nsIRunnable* aTask,
|
|
|
|
uint32_t aTimeoutMs,
|
|
|
|
nsIThread* aTarget = nullptr);
|
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
NS_IMETHOD Notify(nsITimer *timer) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~SimpleTimer() {}
|
|
|
|
nsresult Init(nsIRunnable* aTask, uint32_t aTimeoutMs, nsIThread* aTarget);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> mTask;
|
2015-09-29 03:06:11 +03:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
};
|
|
|
|
|
2015-09-29 03:06:14 +03:00
|
|
|
void
|
|
|
|
LogToBrowserConsole(const nsAString& aMsg);
|
|
|
|
|
2015-10-05 05:03:48 +03:00
|
|
|
bool
|
|
|
|
ParseCodecsString(const nsAString& aCodecs, nsTArray<nsString>& aOutCodecs);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsH264ContentType(const nsAString& aContentType);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsAACContentType(const nsAString& aContentType);
|
|
|
|
|
2015-10-06 06:52:58 +03:00
|
|
|
bool
|
|
|
|
IsAACCodecString(const nsAString& aCodec);
|
|
|
|
|
2016-05-26 12:24:35 +03:00
|
|
|
template <typename String>
|
|
|
|
class StringListRange
|
|
|
|
{
|
|
|
|
typedef typename String::char_type CharType;
|
|
|
|
typedef const CharType* Pointer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Iterator into range, trims items and skips empty items.
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator!=(const Iterator& a) const
|
|
|
|
{
|
|
|
|
return mStart != a.mStart || mEnd != a.mEnd;
|
|
|
|
}
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
SearchItemAt(mComma + 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
typedef decltype(Substring(Pointer(), Pointer())) DereferencedType;
|
|
|
|
DereferencedType operator*()
|
|
|
|
{
|
|
|
|
return Substring(mStart, mEnd);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class StringListRange;
|
|
|
|
Iterator(const CharType* aRangeStart, uint32_t aLength)
|
|
|
|
: mRangeEnd(aRangeStart + aLength)
|
|
|
|
{
|
|
|
|
SearchItemAt(aRangeStart);
|
|
|
|
}
|
|
|
|
void SearchItemAt(Pointer start)
|
|
|
|
{
|
|
|
|
// First, skip leading whitespace.
|
|
|
|
for (Pointer p = start; ; ++p) {
|
|
|
|
if (p >= mRangeEnd) {
|
|
|
|
mStart = mEnd = mComma = mRangeEnd;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto c = *p;
|
|
|
|
if (c == CharType(',')) {
|
|
|
|
// Comma -> Empty item -> Skip.
|
|
|
|
} else if (c != CharType(' ')) {
|
|
|
|
mStart = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Find comma, recording start of trailing space.
|
|
|
|
Pointer trailingWhitespace = nullptr;
|
|
|
|
for (Pointer p = mStart + 1; ; ++p) {
|
|
|
|
if (p >= mRangeEnd) {
|
|
|
|
mEnd = trailingWhitespace ? trailingWhitespace : p;
|
|
|
|
mComma = p;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto c = *p;
|
|
|
|
if (c == CharType(',')) {
|
|
|
|
mEnd = trailingWhitespace ? trailingWhitespace : p;
|
|
|
|
mComma = p;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c == CharType(' ')) {
|
|
|
|
// Found a whitespace -> Record as trailing if not first one.
|
|
|
|
if (!trailingWhitespace) {
|
|
|
|
trailingWhitespace = p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Found a non-whitespace -> Reset trailing whitespace if needed.
|
|
|
|
if (trailingWhitespace) {
|
|
|
|
trailingWhitespace = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const Pointer mRangeEnd;
|
|
|
|
Pointer mStart;
|
|
|
|
Pointer mEnd;
|
|
|
|
Pointer mComma;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit StringListRange(const String& aList) : mList(aList) {}
|
|
|
|
Iterator begin()
|
|
|
|
{
|
|
|
|
return Iterator(mList.Data(), mList.Length());
|
|
|
|
}
|
|
|
|
Iterator end()
|
|
|
|
{
|
|
|
|
return Iterator(mList.Data() + mList.Length(), 0);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const String& mList;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename String>
|
|
|
|
StringListRange<String>
|
|
|
|
MakeStringListRange(const String& aList)
|
|
|
|
{
|
|
|
|
return StringListRange<String>(aList);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ListString, typename ItemString>
|
|
|
|
static bool
|
|
|
|
StringListContains(const ListString& aList, const ItemString& aItem)
|
|
|
|
{
|
|
|
|
for (const auto& listItem : MakeStringListRange(aList)) {
|
|
|
|
if (listItem.Equals(aItem)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-04 02:45:19 +04:00
|
|
|
} // end namespace mozilla
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
#endif
|