From:	CSBVAX::MRGATE!info-gnu-emacs-request@prep.ai.mit.edu@SMTP 21-JUL-1988 02:07
To:	ARISIA::EVERHART
Subj:	subst-char-in-string function in C


Received: from TUT.CIS.OHIO-STATE.EDU by prep.ai.mit.edu; Wed, 20 Jul 88 23:23:05 EST
Received: by tut.cis.ohio-state.edu (5.54/2.0)
	id AA24579; Thu, 21 Jul 88 00:04:15 EDT
Received: from USENET by tut.cis.ohio-state.edu with netnews
	for info-gnu-emacs@prep.ai.mit.edu (info-gnu-emacs@prep.ai.mit.edu)
	(contact usenet@tut.cis.ohio-state.edu if you have questions)
Date: 21 Jul 88 00:39:21 GMT
From: uhccux!julian@humu.nosc.mil  (Julian Cowley)
Organization: University of Hawaii at Manoa
Subject: subst-char-in-string function in C
Message-Id: <2096@uhccux.uhcc.hawaii.edu>
Sender: info-gnu-emacs-request@prep.ai.mit.edu
To: info-gnu-emacs@prep.ai.mit.edu

[ There seems to be no standard way to contribute source code to the
GNU distribution, yet the FSF says they encourage modifications and
additions.  I've seen numerous sources pass by in these newsgroups
that would be great to have in the distribution, but they always seem
to be ignored.  Nevertheless, here's one more! -- jlc ]

This is a C function for GNU Emacs that substitutes one char for
another in a string.  Since there is a subst-char-in-region, I thought
that it would be nice to have a counterpart for strings.  This is my
first attempt at writing modifications directly into the source code,
so I would appreciate any pointers from people who already have
experience doing this.

subst-char-in-string:
Directly modify STRING in memory, replacing each instance of FROMCHAR
with TOCHAR.  If you don't want to directly modify the string, first use
copy-sequence to make a copy of it.

This diff is for the version 18.51 fns.c source file, but it should
work for any version, hopefully.  To apply it, you should use Larry
Wall's patch program and then redump Emacs.  Enjoy!

*** fns.c.orig	Wed Jul 20 13:50:31 1988
--- fns.c	Wed Jul 20 14:31:40 1988
***************
*** 421,426
  
    return val;
  }
  
  DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
    "Takes cdr N times on LIST, returns the result.")

--- 421,457 -----
  
    return val;
  }
+ 
+ DEFUN ("subst-char-in-string", Fsubst_char_in_string,
+        Ssubst_char_in_string, 3, 3, 0,
+   "Directly modify STRING in memory, replacing each instance of FROMCHAR\n\
+ with TOCHAR.  If you don't want to directly modify the string, first use\n\
+ copy-sequence to make a copy of it."
+   (str, fromchar, tochar)
+      Lisp_Object str, fromchar, tochar;
+ {
+   register unsigned char *p, *pend;
+   register unsigned char from, to;
+ 
+   CHECK_STRING (str, 0);
+   CHECK_NUMBER (fromchar, 1);
+   CHECK_NUMBER (tochar, 2);
+ 
+   from = XINT (fromchar);
+   to = XINT (tochar);
+ 
+   p = XSTRING (str)->data;
+   pend = p + XSTRING (str)->size;
+ 
+   while (p != pend)
+     {
+       if (*p == from) *p = to; p++;
+ 	/* is this appropriate here? -- jlc */
+       QUIT;
+     }
+ 
+   return str;
+ }
  
  DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
    "Takes cdr N times on LIST, returns the result.")
***************
*** 1320,1325
    defsubr (&Scopy_sequence);
    defsubr (&Scopy_alist);
    defsubr (&Ssubstring);
    defsubr (&Snthcdr);
    defsubr (&Snth);
    defsubr (&Selt);

--- 1351,1357 -----
    defsubr (&Scopy_sequence);
    defsubr (&Scopy_alist);
    defsubr (&Ssubstring);
+   defsubr (&Ssubst_char_in_string);
    defsubr (&Snthcdr);
    defsubr (&Snth);
    defsubr (&Selt);

Julian Cowley, U. of Hawaii at Manoa
julian@uhccux.uhcc.hawaii.edu
{uunet,ucbvax}!ucsd!nosc!uhccux!julian
julian@uhccux.bitnet
