Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!bloom-beacon.mit.edu!gatech!swrinde!pipex!uunet!news.cygnus.com!nntp!lord
From: lord@x1.cygnus.com (Tom Lord)
Subject: Re: why no substring sharing?
In-Reply-To: snark@bark.COM's message of 26 Nov 1994 17:27:13 -0500
Message-ID: <LORD.94Nov26220238@x1.cygnus.com>
Sender: news@cygnus.com
Nntp-Posting-Host: x1.cygnus.com
Organization: Cygnus Support
References: snark@bark.COM (Impatient Observer) <9411262226.AA13273@bitsy.MIT.EDU>
Date: Sun, 27 Nov 1994 06:02:37 GMT
Lines: 55



  snark@bark.COM (Impatient Observer) writes:
   > schwartz@galapagos.cse.psu.edu (Scott Schwartz) writes:
   > Organization: Penn State Comp Sci & Eng
   >
   > Why does substring return a newly allocated string?

   Because strings in Scheme, like vectors, have an object header which
   contains their type code and length. To make every substring of a string
   carry this kind of information would be wasteful. If you want this sort of
   behavior you can always use string->list to get back a list of characters
   which you can then hack as you detailed.


I don't think that's a good answer.

SUBSTRING returns a newly allocated string so that subsequent
modifications to either one of the original string or the substring
don't effect the other.  If your string primitives can implement
copy-on-write semantics, then SUBSTRING doesn't really have to do much
allocation (right away).

SHARED-SUBSTRING, SHARED-SUBSTRING?, and UNSHARE-SUBSTRING!  would do
a lot to help make Scheme programs that manipulate strings
efficiently. 

	(SHARED-SUBSTRING <string> <start> <end>) => <string>

		Returns a string that contains a substring of the
		first argument.  The result and the first argument
		_may_ share state but aren't required to.  If the
		program checks whether a string is truly shard, either
		by modifying strings or by using SHARED-SUBSTRING?,
		then the answer should always be the same except
		as modified by explicit calls to UNSHARE-SUBSTRING!.
		An implementation must be consistent: if SHARED-SUBSTRING
		ever returns a state-sharing string, then it should
		always do so.

	(SHARED-SUBSTRING? <string>) => <boolean>

		Returns true if there exist other live strings
		that share state with the argument.

	(UNSHARE-SUBSTRING! <string>) => <unspecified>

		Modify the argument to no longer share state with
		any other string in the system.

--
----

If you would like to volunteer to help with the GNU extension language
project, please write to lord@gnu.ai.mit.edu.
