changeset 20872:431f875501a6

(byte_char_debug_check): New function. (CONSIDER, both definitions): Call it. (buf_charpos_to_bytepos, buf_bytepos_to_charpos): Likewise. (byte_debug_flag): New variable. (syms_of_marker): Set up Lisp variable.
author Richard M. Stallman <rms@gnu.org>
date Sun, 08 Feb 1998 22:40:44 +0000
parents adb8a0943fc1
children d2b5d0e2607e
files src/marker.c
diffstat 1 files changed, 66 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/marker.c	Sun Feb 08 21:35:54 1998 +0000
+++ b/src/marker.c	Sun Feb 08 22:40:44 1998 +0000
@@ -32,6 +32,10 @@
 static struct buffer *cached_buffer;
 static int cached_modiff;
 
+/* Nonzero means enable debugging checks on byte/char correspondences.  */
+
+static int byte_debug_flag;
+
 clear_charpos_cache (b)
      struct buffer *b;
 {
@@ -58,7 +62,12 @@
   int changed = 0;							\
 									\
   if (this_charpos == charpos)						\
-    return (BYTEPOS);							\
+    {									\
+      int value = (BYTEPOS);						\
+      if (byte_debug_flag)						\
+	byte_char_debug_check (b, charpos, value);			\
+      return value;							\
+    }									\
   else if (this_charpos > charpos)					\
     {									\
       if (this_charpos < best_above)					\
@@ -78,11 +87,37 @@
   if (changed)								\
     {									\
       if (best_above - best_below == best_above_byte - best_below_byte)	\
-	return best_below_byte + (charpos - best_below);		\
+        {								\
+	  int value = best_below_byte + (charpos - best_below);		\
+	  if (byte_debug_flag)						\
+	    byte_char_debug_check (b, charpos, value);			\
+	  return value;							\
+	}								\
     }									\
 }
 
 int
+byte_char_debug_check (b, charpos, bytepos)
+     struct buffer *b;
+     int charpos, bytepos;
+{
+  int nchars = 0;
+
+  if (bytepos > BUF_GPT_BYTE (b))
+    {
+      nchars = chars_in_text (BUF_BEG_ADDR (b),
+			      BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b));
+      nchars += chars_in_text (BUF_GAP_END_ADDR (b),
+			       bytepos - BUF_GPT_BYTE (b));
+    }
+  else
+    nchars = chars_in_text (BUF_BEG_ADDR (b), bytepos - BUF_BEG_BYTE (b));
+
+  if (charpos - 1 != nchars)
+    abort ();
+}
+
+int
 charpos_to_bytepos (charpos)
      int charpos;
 {
@@ -169,6 +204,9 @@
 	  set_marker_both (marker, Qnil, best_below, best_below_byte);
 	}
 
+      if (byte_debug_flag)
+	byte_char_debug_check (b, charpos, best_below_byte);
+
       cached_buffer = b;
       cached_modiff = BUF_MODIFF (b);
       cached_charpos = best_below;
@@ -196,6 +234,9 @@
 	  set_marker_both (marker, Qnil, best_above, best_above_byte);
 	}
 
+      if (byte_debug_flag)
+	byte_char_debug_check (b, charpos, best_above_byte);
+
       cached_buffer = b;
       cached_modiff = BUF_MODIFF (b);
       cached_charpos = best_above;
@@ -218,7 +259,12 @@
   int changed = 0;							\
 									\
   if (this_bytepos == bytepos)						\
-    return (CHARPOS);							\
+    {									\
+      int value = (CHARPOS);						\
+      if (byte_debug_flag)						\
+	byte_char_debug_check (b, value, bytepos);			\
+      return value;							\
+    }									\
   else if (this_bytepos > bytepos)					\
     {									\
       if (this_bytepos < best_above_byte)				\
@@ -238,7 +284,12 @@
   if (changed)								\
     {									\
       if (best_above - best_below == best_above_byte - best_below_byte)	\
-	return best_below + (bytepos - best_below_byte);		\
+	{								\
+	  int value = best_below + (bytepos - best_below_byte);		\
+	  if (byte_debug_flag)						\
+	    byte_char_debug_check (b, value, bytepos);			\
+	  return value;							\
+	}								\
     }									\
 }
 
@@ -319,6 +370,9 @@
 	  set_marker_both (marker, Qnil, best_below, best_below_byte);
 	}
 
+      if (byte_debug_flag)
+	byte_char_debug_check (b, best_below, bytepos);
+
       cached_buffer = b;
       cached_modiff = BUF_MODIFF (b);
       cached_charpos = best_below;
@@ -346,6 +400,9 @@
 	  set_marker_both (marker, Qnil, best_above, best_above_byte);
 	}
 
+      if (byte_debug_flag)
+	byte_char_debug_check (b, best_above, bytepos);
+
       cached_buffer = b;
       cached_modiff = BUF_MODIFF (b);
       cached_charpos = best_above;
@@ -838,4 +895,9 @@
   defsubr (&Smarker_insertion_type);
   defsubr (&Sset_marker_insertion_type);
   defsubr (&Sbuffer_has_markers_at);
+
+  DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag,
+   "Non-nil enables debugging checks in byte/char position conversions.");
+  byte_debug_flag = 0;
+
 }