From 0e3c9caae54a7dabbb97f96eec3cd5ef4c555a87 Mon Sep 17 00:00:00 2001 From: "mike@reedtribe.org" Date: Wed, 4 Jan 2012 11:37:46 +0000 Subject: [PATCH] add count(), fix bad llist logic in remove() git-svn-id: http://skia.googlecode.com/svn/trunk@2958 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/utils/SkJSON.h | 6 ++++++ src/utils/SkJSON.cpp | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/utils/SkJSON.h b/include/utils/SkJSON.h index 215d54a604..5268af53cd 100644 --- a/include/utils/SkJSON.h +++ b/include/utils/SkJSON.h @@ -76,6 +76,12 @@ public: */ void addBool(const char name[], bool value); + /** + * Return the number of slots/fields in this object. These can be + * iterated using Iter. + */ + int count() const; + /** * Returns true if a slot matching the name and Type is found. */ diff --git a/src/utils/SkJSON.cpp b/src/utils/SkJSON.cpp index 6636c69d05..c55d46483e 100644 --- a/src/utils/SkJSON.cpp +++ b/src/utils/SkJSON.cpp @@ -206,6 +206,14 @@ SkJSON::Object::~Object() { LEAK_CODE(SkASSERT(gObjectCount > 0); SkDebugf("~object[%d]\n", --gObjectCount);) } +int SkJSON::Object::count() const { + int n = 0; + for (const Slot* slot = fHead; slot; slot = slot->fNext) { + n += 1; + } + return n; +} + SkJSON::Object::Slot* SkJSON::Object::addSlot(Slot* slot) { SkASSERT(NULL == slot->fNext); if (NULL == fHead) { @@ -327,23 +335,30 @@ bool SkJSON::Object::findBool(const char name[], bool* value) const { } bool SkJSON::Object::remove(const char name[], Type t) { + SkDEBUGCODE(int count = this->count();) Slot* prev = NULL; Slot* slot = fHead; while (slot) { Slot* next = slot->fNext; if (t == slot->type() && !strcmp(slot->name(), name)) { - if (fHead == slot) { + if (prev) { + SkASSERT(fHead != slot); + prev->fNext = next; + } else { + SkASSERT(fHead == slot); fHead = next; } if (fTail == slot) { - fTail = next; + fTail = prev; } delete slot; + SkASSERT(count - 1 == this->count()); return true; } prev = slot; slot = next; } + SkASSERT(count == this->count()); return false; }