LISTSERV mailing list manager LISTSERV 16.0

Help for ZNG Archives


ZNG Archives

ZNG Archives


[email protected]


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

ZNG Home

ZNG Home

ZNG  August 2006

ZNG August 2006

Subject:

Re: substring proposal

From:

Mike Rylander <[log in to unmask]>

Reply-To:

SRU (Search and Retrieve Via URL) Implementors

Date:

Thu, 17 Aug 2006 10:20:43 -0400

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (128 lines)

On 8/17/06, Mike Taylor <[log in to unmask]> wrote:
> Ray Denenberg writes:
>  > I'm trying to track down why we changed it from length to position.
>  > The discussion was among the Ed. Board, and as I recall there was a
>  > good reason but I haven't tracked it down yet (in the Ed. Board
>  > archive). If I can't find it we'll go with position. (Thoug that
>  > does destabalize the proposal.)  The zero-base issue is more
>  > difficult, as it seems there is fairly overwhelming support for
>  > 1-based as far as I can see.
>
> I only agreed to 1-based because someone on list list asserted that
> it's what most substring operations use.  But if Mike Rylander would
> like to repeat his helpful survey of other environments' substring
> operations, I'd support whichever index-base is most widely used
> elsewhere.

Now look what I've gotten myself into ... ;)

I'll go through the environments I use most, in no particular order whatsoever.

There is some amount of discordance between different SQL
implementations regarding the index base of the start position.

Postgresql uses 1-based indexes in order to provide consistency with
ordinal tuple position[1].  It does not allow negative length, but
using negative (or zero) start position will place the start logically
before the beginning of the string, thusly:

poop=# select substr('123456',-2,5);
  substr
--------
 12
(1 row)

While that does have a brand of internal consistency, I wouldn't
promote it here.

Oracle[2], while being mostly 1-based also, allows a start position of
zero which means exactly the same thing as 1.  I think this is worse
than Pg's plan, since there's less consistency.

Just to add more inconsistency, MySQL[3] has decided to allow negative
positions and uses that to mean "from the end of the string", however,
the second parameter is still always length.  They use 1-based start
positions, and don't say anything about zero (and I don't use MySQL),
so I can't comment on that piece.

So that covers SQL implementations, but it doesn't really give much
direction since there is so much disagreement over details.  Arguably,
Pg is the closest to the SQL spec, but the spec does not constrain
extensions in most cases, so it's hard to point to anything solid,
other than nominally 1-based with length.

On to programming languages!

We've seen Perl, PHP, C/C++ (0-based, length) and Python (no substr,
just slice) ... another currently popular language, Ruby[4], uses
0-based start position and length for substr, and /also/ uses 0-based
start position for slices when the '[]' method is handed a range (
x..y ) instead of two numbers.  Java[5], trying to be a rebel, uses
the second param in its .substring() method for end-position (that's a
/slice/, Sun!), but it is also 0-based.  XPath[6] uses 1-based start
position and length.

So here are my ill considered, biased conclusions:

Traditionally, string indexing is 0-based.  This comes from looking at
strings as arrays of characters, and for the purpose of extraction
(with the goal of matching), this feels "right" to me.  Those that
buck real-programming convention and use 1-based position are doing so
for local consistency (SQL -- 1-based ordinal position) or just
because (Java -- any idea?) or because they are following one (or
both) of the 1-based crowd (XPath -- following Java, and attempting to
be a query language like SQL).

Being a programmer, I'm more comfortable with 0-based string indexing,
so that's what my heart would prefer.  However, we are designing a
query language here, so it makes sense to follow the convention of
other query languages.  Both SQL and XPath/XSLT have 1-based substring
implementations, so I'm not going to put up too much of a fight to get
0-base substrings.  I would, however, urge everyone to discard the
idea of "logical" starting positions -- no zero or negatives, please!
There's just too much confusion over what they should mean, and there
are other methods that can be employed for providing "match the end of
the string" stuff.

As to lenghth vs end-position, every single implementation of substr
uses length, with the exception of slice methods that happen to be
spelled "substr".  IMHO, if an end-position version of this method is
desired/desirable, it should be called "slice".  Note, however, that
you can implement slice via substr by saying

  slice( string, substr-start, substr-start + substr-len - 1 )

and substr via slice with

  substr( string, slice-start, slice-end - slice-start + 1 )

So, if that's how you want to do it under the covers, or your language
of choice doesn't have a proper substr implementation, you can
transform to slice in the backend.


1) http://www.postgresql.org/docs/8.1/interactive/functions-string.html
2) http://www.adp-gmbh.ch/ora/sql/substr.html
3) http://mysql.com/doc/refman/5.0/en/string-functions.html
4) http://www.rubycentral.com/book/ref_c_string.html#String._ob_cb
5) http://mindprod.com/jgloss/substring.html
6) http://www.topxml.com/xsl/funcsubstring.asp


>
> (And for the same reason I prefer length to end.)
>
>  _/|_    ___________________________________________________________________
> /o ) \/  Mike Taylor  <[log in to unmask]>  http://www.miketaylor.org.uk
> )_v__/\  "He looks around; he sees angels in the architecture, spinning
>          in infinity" -- Paul Simon, "Call Me Al"
>


-- 
Mike Rylander
[log in to unmask]
GPLS -- PINES Development
Database Developer
http://open-ils.org

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

July 2017
October 2016
July 2016
August 2014
February 2014
December 2013
November 2013
October 2013
February 2013
January 2013
October 2012
August 2012
April 2012
January 2012
October 2011
May 2011
April 2011
November 2010
October 2010
September 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
October 2009
September 2009
August 2009
July 2009
May 2009
April 2009
March 2009
February 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001

ATOM RSS1 RSS2



LISTSERV.LOC.GOV

CataList Email List Search Powered by the LISTSERV Email List Manager