String.st
changeset 51 9b7ae5e18f3e
parent 42 e33491f6f260
child 53 77ed1ef5c018
equal deleted inserted replaced
50:71f3b9444905 51:9b7ae5e18f3e
    20 String comment:'
    20 String comment:'
    21 
    21 
    22 COPYRIGHT (c) 1988 by Claus Gittinger
    22 COPYRIGHT (c) 1988 by Claus Gittinger
    23              All Rights Reserved
    23              All Rights Reserved
    24 
    24 
    25 $Header: /cvs/stx/stx/libbasic/String.st,v 1.9 1994-01-12 19:11:34 claus Exp $
    25 $Header: /cvs/stx/stx/libbasic/String.st,v 1.10 1994-02-05 12:27:25 claus Exp $
    26 '!
    26 '!
    27 
    27 
    28 %{
    28 %{
    29 #include <stdio.h>
    29 #include <stdio.h>
    30 #include <ctype.h>
    30 #include <ctype.h>
   371 !
   371 !
   372 
   372 
   373 storeString
   373 storeString
   374     "return a String for storing myself"
   374     "return a String for storing myself"
   375 
   375 
   376     |s|
   376     |s n index|
   377 
   377 
   378     (self includes:$') ifTrue:[
   378     n := self occurrencesOf:$'.
   379         s := ''''.
   379     n == 0 ifFalse:[
       
   380 	s := String new:(n + 2 + self size).
       
   381         s at:1 put:''''.
       
   382 	index := 2.
   380         self do:[:thisChar |
   383         self do:[:thisChar |
   381             (thisChar == $') ifTrue:[s := s , ''''].
   384             (thisChar == $') ifTrue:[
   382             s := s copyWith:thisChar
   385 		s at:index put:thisChar.
       
   386 		index := index + 1.
       
   387 	    ].
       
   388 	    s at:index put:thisChar.
       
   389 	    index := index + 1.
   383         ].
   390         ].
   384         s := s , ''''.
   391         s at:index put:''''.
   385         ^ s
   392         ^ s
   386     ].
   393     ].
   387     ^ '''' asString , self , '''' asString
   394     ^ '''' asString , self , '''' asString
   388 !
   395 !
   389 
   396 
  1622             *dstp++ = _intVal(_characterVal(aCharacter));
  1629             *dstp++ = _intVal(_characterVal(aCharacter));
  1623             *dstp = '\0';
  1630             *dstp = '\0';
  1624             RETURN (newString );
  1631             RETURN (newString );
  1625         }
  1632         }
  1626     }
  1633     }
  1627 %}
  1634 %}.
  1628 .
  1635     "fall back in case of non-character arg;
       
  1636      will eventually lead to an bad element signal raise"
       
  1637 
  1629     ^ super copyWith:aCharacter
  1638     ^ super copyWith:aCharacter
  1630 !
  1639 !
  1631 
  1640 
  1632 copyFrom:start to:stop
  1641 copyFrom:start to:stop
  1633     "return the substring starting at index start, anInteger and ending
  1642     "return the substring starting at index start, anInteger and ending
  1677                     RETURN ( newString );
  1686                     RETURN ( newString );
  1678                 }
  1687                 }
  1679             }
  1688             }
  1680         }
  1689         }
  1681     }
  1690     }
  1682 %}
  1691 %}.
  1683 .
  1692     "fall back in case of non-integer index or out-of-bound index;
       
  1693      will eventually lead to an out-of-bound signal raise"
       
  1694 
  1684     ^ super copyFrom:start to:stop
  1695     ^ super copyFrom:start to:stop
  1685 !
  1696 !
  1686 
  1697 
  1687 copyFrom:start
  1698 copyFrom:start
  1688     "return the substring from start, anInteger to the end
  1699     "return the substring from start, anInteger to the end
  1728                     RETURN ( newString );
  1739                     RETURN ( newString );
  1729                 }
  1740                 }
  1730             }
  1741             }
  1731         }
  1742         }
  1732     }
  1743     }
  1733 %}
  1744 %}.
  1734 .
  1745     "fall back in case of non-integer index or out-of-bound index;
       
  1746      will eventually lead to an out-of-bound signal raise"
       
  1747 
  1735     ^ super copyFrom:start
  1748     ^ super copyFrom:start
  1736 ! !
  1749 ! !
  1737 
  1750 
  1738 !String methodsFor:'filling and replacing'!
  1751 !String methodsFor:'filling and replacing'!
  1739 
  1752 
  1913 withoutSpaces
  1926 withoutSpaces
  1914     "return a copy of myself without leading and trailing spaces"
  1927     "return a copy of myself without leading and trailing spaces"
  1915 
  1928 
  1916     |startIndex "{ Class: SmallInteger }"
  1929     |startIndex "{ Class: SmallInteger }"
  1917      endIndex   "{ Class: SmallInteger }" 
  1930      endIndex   "{ Class: SmallInteger }" 
  1918      blank|
  1931      sz blank|
  1919 
  1932 
  1920     startIndex := 1.
  1933 %{
  1921     endIndex := self size.
  1934     REGISTER unsigned char *cp0;
  1922     blank := Character space.
  1935     REGISTER unsigned char *cp;
  1923     [(startIndex < endIndex) and:[(self at:startIndex) == blank]] whileTrue:[
  1936 
  1924         startIndex := startIndex + 1
  1937     /* ignore instances of subclasses ... */
  1925     ].
  1938     if (_qClass(self) == String) {
  1926     [(endIndex > 1) and:[(self at:endIndex) == blank]] whileTrue:[
  1939 	cp = cp0 = _stringVal(self);
  1927         endIndex := endIndex - 1
  1940 	while (*cp == ' ') cp++;
       
  1941 	startIndex = _MKSMALLINT(cp - cp0 + 1);
       
  1942 	cp = cp + strlen(cp) - 1;
       
  1943 	while ((cp >= cp0) && (*cp == ' ')) cp--;
       
  1944 	endIndex = _MKSMALLINT(cp - cp0 + 1);
       
  1945     }
       
  1946 %}
       
  1947 .
       
  1948     sz := self size.
       
  1949     startIndex isNil ifTrue:[
       
  1950         startIndex := 1.
       
  1951         endIndex := sz.
       
  1952         blank := Character space.
       
  1953         [(startIndex < endIndex) and:[(self at:startIndex) == blank]] whileTrue:[
       
  1954             startIndex := startIndex + 1
       
  1955         ].
       
  1956         [(endIndex > 1) and:[(self at:endIndex) == blank]] whileTrue:[
       
  1957             endIndex := endIndex - 1
       
  1958         ]
  1928     ].
  1959     ].
  1929     startIndex > endIndex ifTrue:[
  1960     startIndex > endIndex ifTrue:[
  1930         ^ ''
  1961         ^ ''
  1931     ].
  1962     ].
  1932     ((startIndex == 1) and:[endIndex == self size]) ifTrue:[
  1963     ((startIndex == 1) and:[endIndex == sz]) ifTrue:[
  1933         ^ self
  1964         ^ self
  1934     ].
  1965     ].
  1935     ^ self copyFrom:startIndex to:endIndex
  1966     ^ self copyFrom:startIndex to:endIndex
  1936 !
  1967 !
  1937 
  1968 
  1938 withoutSeparators
  1969 withoutSeparators
  1939     "return a copy of myself without leading and trailing whitespace"
  1970     "return a copy of myself without leading and trailing whitespace"
  1940 
  1971 
  1941     |startIndex "{ Class: SmallInteger }"
  1972     |startIndex "{ Class: SmallInteger }"
  1942      endIndex   "{ Class: SmallInteger }" |
  1973      endIndex   "{ Class: SmallInteger }" 
  1943 
  1974      sz|
  1944     startIndex := 1.
  1975 
  1945     endIndex := self size.
  1976 %{
  1946     [(startIndex < endIndex) and:[(self at:startIndex) isSeparator]] whileTrue:[
  1977     REGISTER unsigned char *cp0;
  1947         startIndex := startIndex + 1
  1978     REGISTER unsigned char *cp;
  1948     ].
  1979     REGISTER unsigned char c;
  1949     [(endIndex > 1) and:[(self at:endIndex) isSeparator]] whileTrue:[
  1980 
  1950         endIndex := endIndex - 1
  1981     /* ignore instances of subclasses ... */
       
  1982     if (_qClass(self) == String) {
       
  1983         cp = cp0 = _stringVal(self);
       
  1984 	c = *cp;
       
  1985         while ((c == ' ') || (c == '\n') || (c == '\t')
       
  1986                           || (c == '\r') || (c == '\f')) {
       
  1987 	    cp++;
       
  1988 	    c = *cp;
       
  1989 	}
       
  1990         startIndex = _MKSMALLINT(cp - cp0 + 1);
       
  1991         cp = cp + strlen(cp) - 1;
       
  1992         while ((cp >= cp0) && (*cp == ' ')) cp--;
       
  1993 	c = *cp;
       
  1994         while ((cp >= cp0) &&
       
  1995 	       ((c == ' ') || (c == '\n') || (c == '\t')
       
  1996                            || (c == '\r') || (c == '\f'))) {
       
  1997 	    cp--;
       
  1998 	    c = *cp;
       
  1999 	}
       
  2000         endIndex = _MKSMALLINT(cp - cp0 + 1);
       
  2001     }
       
  2002 %}
       
  2003 .
       
  2004     sz := self size.
       
  2005     startIndex isNil ifTrue:[
       
  2006         startIndex := 1.
       
  2007         endIndex := self size.
       
  2008         [(startIndex < endIndex) and:[(self at:startIndex) isSeparator]] whileTrue:[
       
  2009             startIndex := startIndex + 1
       
  2010         ].
       
  2011         [(endIndex > 1) and:[(self at:endIndex) isSeparator]] whileTrue:[
       
  2012             endIndex := endIndex - 1
       
  2013         ].
  1951     ].
  2014     ].
  1952     startIndex > endIndex ifTrue:[
  2015     startIndex > endIndex ifTrue:[
  1953         ^ ''
  2016         ^ ''
  1954     ].
  2017     ].
  1955     ((startIndex == 1) and:[endIndex == self size]) ifTrue:[
  2018     ((startIndex == 1) and:[endIndex == sz]) ifTrue:[
  1956         ^ self
  2019         ^ self
  1957     ].
  2020     ].
  1958     ^ self copyFrom:startIndex to:endIndex
  2021     ^ self copyFrom:startIndex to:endIndex
  1959 ! !
  2022 ! !
  1960 
  2023