root/trunk/Library/JavaString.fss @ 2739

Revision 2739, 4.9 KB (checked in by black, 15 months ago)

Next iteration of CordedStrings?, with substring nodes. Comparison of Strings with substring nodes is a work in progress. This commit fixes bugs in the Range comparison code that were impeding that progress

Line 
1(*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************)
17
18native component JavaString
19import CordedString.{CatString, EmptyString, margin}
20import FlatStringInfo.{...}
21export JavaString
22
23
24language="java"
25package="com.sun.fortress.interpreter.glue.prim"
26
27  object JavaString
28        extends { String }
29    getter size() : ZZ32 =
30        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Size")
31    getter toString() : String =
32        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$ToString")
33    opr |self| : ZZ32 =
34        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Size")
35       
36    uncheckedSubstring(r1: Range[\ZZ32\]) : String =
37    (* We could build a subString node here, but not until everything is working, because that will intorduce
38       SubString objects into code that may still expect JavaStrings *)
39        if r1.isEmpty then ""
40        else self.javaSubstr(r1.lower, r1.upper + 1)    (* Java substr's endIndex is one past the last character *)
41        end
42       
43    opr =(self, other:String): Boolean =
44        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Eq")
45    opr <(self, other:String): Boolean = self.cmp(other) < 0
46    opr <=(self, other:String): Boolean = self.cmp(other) <= 0
47    opr >(self, other:String): Boolean = self.cmp(other) > 0
48    opr >=(self, other:String): Boolean = self.cmp(other) >= 0
49    opr CMP(self, other:JavaString): TotalComparison = self.cmp(other) CMP 0
50    opr CMP(self, other:String):TotalComparison = INVERSE (other CMP self)
51    opr CASE_INSENSITIVE_CMP(self, other:JavaString): TotalComparison =
52      self.cicmp(other) CMP 0
53    opr CASE_INSENSITIVE_CMP(self, other:String): TotalComparison = INVERSE (other CASE_INSENSITIVE_CMP self)
54
55    (** get skips bounds checking. **)
56    get(i:ZZ32): Char =
57        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Index")
58    cmp(other:JavaString): ZZ32 =
59        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Cmp")
60    cicmp(other:JavaString): ZZ32 =
61        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$CICmp")
62    javaSubstr(lo:ZZ32,hi:ZZ32): String =
63        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$Substr")
64    javaIndexOf(c:Char): ZZ32 =
65        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$IndexOf")
66    javaAppend(self, b:JavaString):String =
67        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$App")
68    javaAppend(self, b:Char):String =
69        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.JavaString$App")
70       
71    (* The following are curently dead code; see lines 3765 ish in FortressLibrary.fss *)   
72(*
73    javaPrint(a: JavaString): () =
74        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.StringPrim$Print")
75    javaPrintln(a: JavaString): () =
76        builtinPrimitive("com.sun.fortress.interpreter.glue.prim.StringPrim$Println")
77*)
78
79    indexOf(c:Char): Maybe[\ZZ32\] = do
80        i = javaIndexOf(c)
81        if i = -1 then Nothing[\ZZ32\] else Just[\ZZ32\](i) end
82      end
83     
84    verify() = do
85        assert(self.depth, 0, self)
86        assert(self.size(), size, self)
87    end
88   
89    showStructure(indent) = do
90        margin(indent)
91        println "J" |self| "/" self.depth ":" self
92    end
93         
94    opr IN(c:Char): Boolean = javaIndexOf(c) =/= -1
95
96    (** The operator %||% with at least one String argument converts to string and
97        appends **)
98    opr ||(self, b:JavaString): String = do
99        bSize = |b|
100        if bSize = 0 then self
101        elif self.size + bSize > flatStringInfo.maxSize then
102            CatString(self, b)
103        else
104            javaAppend(self, b)
105        end
106      end
107
108    opr ||(self, b:String):String =
109    (* It would be good to do something more sophisticated when b is short, such as copying
110       self and b into a StringBuffer and then turning the whole thing into a JavaString *)
111        if self.isEmpty then b else CatString(self, b) end
112       
113 
114    opr ||(self, b:Char): String =
115        if |self| ≥ flatStringInfo.maxSize then
116            CatString(self, b.toString)
117        else
118            javaAppend(self, b)
119        end
120       
121    subdivide() = Nothing[\Generator[\(ZZ32, String)\]\]
122
123  end
124
125end
Note: See TracBrowser for help on using the browser.