xpcom/ds/CharTokenizer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/CharTokenizer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_CharTokenizer_h
    1.10 +#define mozilla_CharTokenizer_h
    1.11 +
    1.12 +#include "nsDependentSubstring.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +
    1.16 +template<char16_t delimiter>
    1.17 +class CharTokenizer
    1.18 +{
    1.19 +public:
    1.20 +    CharTokenizer(const nsSubstring& aSource)
    1.21 +    {
    1.22 +      aSource.BeginReading(mIter);
    1.23 +      aSource.EndReading(mEnd);
    1.24 +    }
    1.25 +
    1.26 +    /**
    1.27 +     * Checks if any more tokens are available.
    1.28 +     */
    1.29 +    bool hasMoreTokens()
    1.30 +    {
    1.31 +      return mIter != mEnd;
    1.32 +    }
    1.33 +
    1.34 +    /**
    1.35 +     * Returns the next token.
    1.36 +     */
    1.37 +    const nsDependentSubstring nextToken()
    1.38 +    {
    1.39 +      nsSubstring::const_char_iterator begin = mIter;
    1.40 +      while (mIter != mEnd && (*mIter) != delimiter) {
    1.41 +        ++mIter;
    1.42 +      }
    1.43 +      nsSubstring::const_char_iterator end = mIter;
    1.44 +      if (mIter != mEnd) {
    1.45 +        ++mIter;
    1.46 +      }
    1.47 +
    1.48 +      return Substring(begin, end);
    1.49 +    }
    1.50 +
    1.51 +private:
    1.52 +    nsSubstring::const_char_iterator mIter, mEnd;
    1.53 +};
    1.54 +
    1.55 +template<char delimiter>
    1.56 +class CCharTokenizer
    1.57 +{
    1.58 +public:
    1.59 +    CCharTokenizer(const nsCSubstring& aSource)
    1.60 +    {
    1.61 +      aSource.BeginReading(mIter);
    1.62 +      aSource.EndReading(mEnd);
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Checks if any more tokens are available.
    1.67 +     */
    1.68 +    bool hasMoreTokens()
    1.69 +    {
    1.70 +      return mIter != mEnd;
    1.71 +    }
    1.72 +
    1.73 +    /**
    1.74 +     * Returns the next token.
    1.75 +     */
    1.76 +    const nsDependentCSubstring nextToken()
    1.77 +    {
    1.78 +      nsCSubstring::const_char_iterator begin = mIter;
    1.79 +      while (mIter != mEnd && (*mIter) != delimiter) {
    1.80 +        ++mIter;
    1.81 +      }
    1.82 +      nsCSubstring::const_char_iterator end = mIter;
    1.83 +      if (mIter != mEnd) {
    1.84 +        ++mIter;
    1.85 +      }
    1.86 +
    1.87 +      return Substring(begin, end);
    1.88 +    }
    1.89 +
    1.90 +private:
    1.91 +    nsCSubstring::const_char_iterator mIter, mEnd;
    1.92 +};
    1.93 +
    1.94 +} // namespace mozilla
    1.95 +
    1.96 +#endif /* mozilla_CharTokenizer_h */

mercurial