toolkit/components/url-classifier/ChunkSet.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ChunkSet.h"
michael@0 7
michael@0 8 namespace mozilla {
michael@0 9 namespace safebrowsing {
michael@0 10
michael@0 11 nsresult
michael@0 12 ChunkSet::Serialize(nsACString& aChunkStr)
michael@0 13 {
michael@0 14 aChunkStr.Truncate();
michael@0 15
michael@0 16 uint32_t i = 0;
michael@0 17 while (i < mChunks.Length()) {
michael@0 18 if (i != 0) {
michael@0 19 aChunkStr.Append(',');
michael@0 20 }
michael@0 21 aChunkStr.AppendInt((int32_t)mChunks[i]);
michael@0 22
michael@0 23 uint32_t first = i;
michael@0 24 uint32_t last = first;
michael@0 25 i++;
michael@0 26 while (i < mChunks.Length() && (mChunks[i] == mChunks[i - 1] + 1 || mChunks[i] == mChunks[i - 1])) {
michael@0 27 last = i++;
michael@0 28 }
michael@0 29
michael@0 30 if (last != first) {
michael@0 31 aChunkStr.Append('-');
michael@0 32 aChunkStr.AppendInt((int32_t)mChunks[last]);
michael@0 33 }
michael@0 34 }
michael@0 35
michael@0 36 return NS_OK;
michael@0 37 }
michael@0 38
michael@0 39 nsresult
michael@0 40 ChunkSet::Set(uint32_t aChunk)
michael@0 41 {
michael@0 42 uint32_t idx = mChunks.BinaryIndexOf(aChunk);
michael@0 43 if (idx == nsTArray<uint32_t>::NoIndex) {
michael@0 44 mChunks.InsertElementSorted(aChunk);
michael@0 45 }
michael@0 46 return NS_OK;
michael@0 47 }
michael@0 48
michael@0 49 nsresult
michael@0 50 ChunkSet::Unset(uint32_t aChunk)
michael@0 51 {
michael@0 52 mChunks.RemoveElementSorted(aChunk);
michael@0 53
michael@0 54 return NS_OK;
michael@0 55 }
michael@0 56
michael@0 57 bool
michael@0 58 ChunkSet::Has(uint32_t aChunk) const
michael@0 59 {
michael@0 60 return mChunks.BinaryIndexOf(aChunk) != nsTArray<uint32_t>::NoIndex;
michael@0 61 }
michael@0 62
michael@0 63 nsresult
michael@0 64 ChunkSet::Merge(const ChunkSet& aOther)
michael@0 65 {
michael@0 66 const uint32_t *dupIter = aOther.mChunks.Elements();
michael@0 67 const uint32_t *end = aOther.mChunks.Elements() + aOther.mChunks.Length();
michael@0 68
michael@0 69 for (const uint32_t *iter = dupIter; iter != end; iter++) {
michael@0 70 nsresult rv = Set(*iter);
michael@0 71 NS_ENSURE_SUCCESS(rv, rv);
michael@0 72 }
michael@0 73
michael@0 74 return NS_OK;
michael@0 75 }
michael@0 76
michael@0 77 nsresult
michael@0 78 ChunkSet::Remove(const ChunkSet& aOther)
michael@0 79 {
michael@0 80 uint32_t *addIter = mChunks.Elements();
michael@0 81 uint32_t *end = mChunks.Elements() + mChunks.Length();
michael@0 82
michael@0 83 for (uint32_t *iter = addIter; iter != end; iter++) {
michael@0 84 if (!aOther.Has(*iter)) {
michael@0 85 *addIter = *iter;
michael@0 86 addIter++;
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 mChunks.SetLength(addIter - mChunks.Elements());
michael@0 91
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94
michael@0 95 void
michael@0 96 ChunkSet::Clear()
michael@0 97 {
michael@0 98 mChunks.Clear();
michael@0 99 }
michael@0 100
michael@0 101 }
michael@0 102 }

mercurial