add count(), fix bad llist logic in remove()
git-svn-id: http://skia.googlecode.com/svn/trunk@2958 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/utils/SkJSON.h b/include/utils/SkJSON.h
index 215d54a..5268af5 100644
--- a/include/utils/SkJSON.h
+++ b/include/utils/SkJSON.h
@@ -77,6 +77,12 @@
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.
*/
bool find(const char name[], Type) const;
diff --git a/src/utils/SkJSON.cpp b/src/utils/SkJSON.cpp
index 6636c69..c55d464 100644
--- a/src/utils/SkJSON.cpp
+++ b/src/utils/SkJSON.cpp
@@ -206,6 +206,14 @@
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::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;
}