Add Rope_length (mostly as a way to add __attribute__((pure)) to the codebase
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 20 Aug 2019 02:28:18 +0000 (22:28 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 20 Aug 2019 02:28:18 +0000 (22:28 -0400)
c/rope.c
c/rope.h

index 716a5af..2cab922 100644 (file)
--- a/c/rope.c
+++ b/c/rope.c
@@ -87,4 +87,13 @@ Rope* Rope_concatenate(Rope* r0, Rope* r1) {
   return result;
 }
 
+__attribute__((pure)) size_t Rope_length(Rope* self) {
+  switch(self->type) {
+    case ROPETYPE_CONCATENATION:
+      return Rope_length(self->instance.concatenation.r0) + Rope_length(self->instance.concatenation.r0);
+    case ROPETYPE_STRING:
+      return self->instance.string.length;
+  }
+}
+
 #endif
index 33319e7..650596d 100644 (file)
--- a/c/rope.h
+++ b/c/rope.h
@@ -21,4 +21,6 @@ Rope* Rope_read(Encoding, FILE);
 
 Rope* Rope_concatenate(Rope* r0, Rope* r1);
 
+size_t Rope_length(Rope*) __attribute__((pure));
+
 #endif