toolkit/components/url-classifier/ChunkSet.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/url-classifier/ChunkSet.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     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 +#include "ChunkSet.h"
    1.10 +
    1.11 +namespace mozilla {
    1.12 +namespace safebrowsing {
    1.13 +
    1.14 +nsresult
    1.15 +ChunkSet::Serialize(nsACString& aChunkStr)
    1.16 +{
    1.17 +  aChunkStr.Truncate();
    1.18 +
    1.19 +  uint32_t i = 0;
    1.20 +  while (i < mChunks.Length()) {
    1.21 +    if (i != 0) {
    1.22 +      aChunkStr.Append(',');
    1.23 +    }
    1.24 +    aChunkStr.AppendInt((int32_t)mChunks[i]);
    1.25 +
    1.26 +    uint32_t first = i;
    1.27 +    uint32_t last = first;
    1.28 +    i++;
    1.29 +    while (i < mChunks.Length() && (mChunks[i] == mChunks[i - 1] + 1 || mChunks[i] == mChunks[i - 1])) {
    1.30 +      last = i++;
    1.31 +    }
    1.32 +
    1.33 +    if (last != first) {
    1.34 +      aChunkStr.Append('-');
    1.35 +      aChunkStr.AppendInt((int32_t)mChunks[last]);
    1.36 +    }
    1.37 +  }
    1.38 +
    1.39 +  return NS_OK;
    1.40 +}
    1.41 +
    1.42 +nsresult
    1.43 +ChunkSet::Set(uint32_t aChunk)
    1.44 +{
    1.45 +  uint32_t idx = mChunks.BinaryIndexOf(aChunk);
    1.46 +  if (idx == nsTArray<uint32_t>::NoIndex) {
    1.47 +    mChunks.InsertElementSorted(aChunk);
    1.48 +  }
    1.49 +  return NS_OK;
    1.50 +}
    1.51 +
    1.52 +nsresult
    1.53 +ChunkSet::Unset(uint32_t aChunk)
    1.54 +{
    1.55 +  mChunks.RemoveElementSorted(aChunk);
    1.56 +
    1.57 +  return NS_OK;
    1.58 +}
    1.59 +
    1.60 +bool
    1.61 +ChunkSet::Has(uint32_t aChunk) const
    1.62 +{
    1.63 +  return mChunks.BinaryIndexOf(aChunk) != nsTArray<uint32_t>::NoIndex;
    1.64 +}
    1.65 +
    1.66 +nsresult
    1.67 +ChunkSet::Merge(const ChunkSet& aOther)
    1.68 +{
    1.69 +  const uint32_t *dupIter = aOther.mChunks.Elements();
    1.70 +  const uint32_t *end = aOther.mChunks.Elements() + aOther.mChunks.Length();
    1.71 +
    1.72 +  for (const uint32_t *iter = dupIter; iter != end; iter++) {
    1.73 +    nsresult rv = Set(*iter);
    1.74 +    NS_ENSURE_SUCCESS(rv, rv);
    1.75 +  }
    1.76 +
    1.77 +  return NS_OK;
    1.78 +}
    1.79 +
    1.80 +nsresult
    1.81 +ChunkSet::Remove(const ChunkSet& aOther)
    1.82 +{
    1.83 +  uint32_t *addIter = mChunks.Elements();
    1.84 +  uint32_t *end = mChunks.Elements() + mChunks.Length();
    1.85 +
    1.86 +  for (uint32_t *iter = addIter; iter != end; iter++) {
    1.87 +    if (!aOther.Has(*iter)) {
    1.88 +      *addIter = *iter;
    1.89 +      addIter++;
    1.90 +    }
    1.91 +  }
    1.92 +
    1.93 +  mChunks.SetLength(addIter - mChunks.Elements());
    1.94 +
    1.95 +  return NS_OK;
    1.96 +}
    1.97 +
    1.98 +void
    1.99 +ChunkSet::Clear()
   1.100 +{
   1.101 +  mChunks.Clear();
   1.102 +}
   1.103 +
   1.104 +}
   1.105 +}

mercurial