From 725afdee94b176cd6c0e4d591745a176f831b0c0 Mon Sep 17 00:00:00 2001
From: cuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Date: Sun, 11 Jul 2004 11:13:10 +0000
Subject: [PATCH] Added SB_Compare

git-svn-id: svn://svn.cc65.org/cc65/trunk@3153 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 src/common/strbuf.c | 28 ++++++++++++++++++++++++++--
 src/common/strbuf.h |  5 ++++-
 2 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/src/common/strbuf.c b/src/common/strbuf.c
index 0adf5a5c3..66c07b875 100644
--- a/src/common/strbuf.c
+++ b/src/common/strbuf.c
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001-2003 Ullrich von Bassewitz                                       */
+/* (C) 2001-2004 Ullrich von Bassewitz                                       */
 /*               R�merstrasse 52                                             */
 /*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
@@ -202,7 +202,7 @@ void SB_AppendChar (StrBuf* B, int C)
     if (NewLen > B->Allocated) {
 	SB_Realloc (B, NewLen);
     }
-    B->Buf[B->Len] = (char) C;            
+    B->Buf[B->Len] = (char) C;
     B->Len = NewLen;
 }
 
@@ -302,3 +302,27 @@ void SB_Move (StrBuf* Target, StrBuf* Source)
 
 
 
+int SB_Compare (const StrBuf* S1, const StrBuf* S2)
+/* Do a lexical compare of S1 and S2. See strcmp for result codes. */
+{       
+    int Result;
+    if (S1->Len < S2->Len) {
+        Result = memcmp (S1->Buf, S2->Buf, S1->Len);
+        if (Result == 0) {
+            /* S1 considered lesser because it's shorter */
+            Result = -1;
+        }
+    } else if (S1->Len > S2->Len) {
+        Result = memcmp (S1->Buf, S2->Buf, S2->Len);
+        if (Result == 0) {
+            /* S2 considered lesser because it's shorter */
+            Result = 1;
+        }
+    } else {
+        Result = memcmp (S1->Buf, S2->Buf, S1->Len);
+    }
+    return Result;
+}
+
+
+
diff --git a/src/common/strbuf.h b/src/common/strbuf.h
index a209f4e9e..5d159cd5f 100644
--- a/src/common/strbuf.h
+++ b/src/common/strbuf.h
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001-2003 Ullrich von Bassewitz                                       */
+/* (C) 2001-2004 Ullrich von Bassewitz                                       */
 /*               R�merstrasse 52                                             */
 /*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
@@ -343,6 +343,9 @@ void SB_Move (StrBuf* Target, StrBuf* Source);
  * contents of Target, and Source will be empty after the call.
  */
 
+int SB_Compare (const StrBuf* S1, const StrBuf* S2);
+/* Do a lexical compare of S1 and S2. See strcmp for result codes. */
+
 
 
 /* End of strbuf.h */