]> gitweb.factorcode.org Git - factor.git/blob - library/platform/jvm/strings.factor
CHAR: notation for literal chars, native parser work
[factor.git] / library / platform / jvm / strings.factor
1 ! :folding=indent:collapseFolds=0:
2
3 ! $Id$
4 !
5 ! Copyright (C) 2004 Slava Pestov.
6
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
9
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 !    this list of conditions and the following disclaimer.
12
13 ! 2. Redistributions in binary form must reproduce the above copyright notice,
14 !    this list of conditions and the following disclaimer in the documentation
15 !    and/or other materials provided with the distribution.
16
17 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 IN: strings
29 USE: kernel
30 USE: logic
31 USE: stack
32
33 : char? ( obj -- boolean )
34     "java.lang.Character" is ;
35
36 : >char ( obj -- char )
37     "char" coerce ; inline
38
39 : string? ( obj -- ? )
40     dup char? swap "java.lang.String" is or ;
41
42 : str-length ( str -- length )
43     [ ] "java.lang.String" "length" jinvoke ;
44
45 : substring ( start end str -- str )
46     [ "int" "int" ] "java.lang.String" "substring"
47     jinvoke ;
48
49 : >str ( obj -- string )
50     ! Returns the Java string representation of this object.
51     [ ] "java.lang.Object" "toString" jinvoke ;
52
53 : >bytes ( string -- array )
54     ! Converts a string to an array of ASCII bytes. An exception
55     ! is thrown if the string contains non-ASCII characters.
56     "ASCII" swap
57     [ "java.lang.String" ] "java.lang.String" "getBytes"
58     jinvoke ;
59
60 : str-nth ( index str -- char )
61     [ "int" ] "java.lang.String" "charAt" jinvoke ;
62
63 : >lower ( str -- str )
64     [ ] "java.lang.String" "toLowerCase" jinvoke ;
65
66 : >upper ( str -- str )
67     [ ] "java.lang.String" "toUpperCase" jinvoke ;
68
69 : index-of* ( index string substring -- index )
70     dup char? [
71         -rot
72         ! Why is the first parameter an int and not a char?
73         [ "int" "int" ]
74         "java.lang.String" "indexOf"
75         jinvoke
76     ] [
77         -rot
78         [ "java.lang.String" "int" ]
79         "java.lang.String" "indexOf"
80         jinvoke
81     ] ifte ;
82
83 : str-compare ( str1 str2 -- n )
84     swap [ "java.lang.String" ] "java.lang.String" "compareTo"
85     jinvoke ;