JS backend: cleanup strings code.
Added String.size and String.length().
This commit is contained in:
@@ -3,12 +3,6 @@ package java.lang
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import js.library
|
import js.library
|
||||||
|
|
||||||
library("splitString")
|
|
||||||
public fun String.split(regex : String) : Array<String> = js.noImpl
|
|
||||||
|
|
||||||
library("splitString")
|
|
||||||
public fun String.split(regex : String, limit: Int) : Array<String> = js.noImpl
|
|
||||||
|
|
||||||
library
|
library
|
||||||
open public class Exception(message: String? = null) : Throwable() {}
|
open public class Exception(message: String? = null) : Throwable() {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
package js
|
package js
|
||||||
|
|
||||||
|
native public fun String.startsWith(s: String): Boolean = noImpl
|
||||||
|
native public fun String.endsWith(s: String): Boolean = noImpl
|
||||||
|
native public fun String.contains(s: String): Boolean = noImpl
|
||||||
|
|
||||||
|
native public fun String.startsWith(char: Char): Boolean = noImpl
|
||||||
|
native public fun String.endsWith(char: Char): Boolean = noImpl
|
||||||
|
native public fun String.contains(char: Char): Boolean = noImpl
|
||||||
|
|
||||||
native public fun String.toUpperCase() : String = js.noImpl
|
native public fun String.toUpperCase() : String = js.noImpl
|
||||||
|
|
||||||
native public fun String.toLowerCase() : String = js.noImpl
|
native public fun String.toLowerCase() : String = js.noImpl
|
||||||
@@ -10,8 +18,11 @@ native public fun String.indexOf(str : String, fromIndex : Int) : Int = js.noImp
|
|||||||
native public fun String.lastIndexOf(str: String) : Int = js.noImpl
|
native public fun String.lastIndexOf(str: String) : Int = js.noImpl
|
||||||
native public fun String.lastIndexOf(str : String, fromIndex : Int) : Int = js.noImpl
|
native public fun String.lastIndexOf(str : String, fromIndex : Int) : Int = js.noImpl
|
||||||
|
|
||||||
// Defined in javalang.kt
|
library("splitString")
|
||||||
//native public fun String.split(regex : String) : Array<String> = js.noImpl
|
public fun String.split(regex: String): Array<String> = js.noImpl
|
||||||
|
|
||||||
|
library("splitString")
|
||||||
|
public fun String.split(regex: String, limit: Int): Array<String> = js.noImpl
|
||||||
|
|
||||||
native public fun String.substring(beginIndex : Int) : String = js.noImpl
|
native public fun String.substring(beginIndex : Int) : String = js.noImpl
|
||||||
native public fun String.substring(beginIndex : Int, endIndex : Int) : String = js.noImpl
|
native public fun String.substring(beginIndex : Int, endIndex : Int) : String = js.noImpl
|
||||||
@@ -24,9 +35,10 @@ native public fun String.match(regex : String) : Array<String> = js.noImpl
|
|||||||
|
|
||||||
native public fun String.trim() : String = js.noImpl
|
native public fun String.trim() : String = js.noImpl
|
||||||
|
|
||||||
native public val String.length : Int
|
public val String.size: Int
|
||||||
get() = js.noImpl
|
get() = js.noImpl
|
||||||
|
|
||||||
|
public fun String.length(): Int = js.noImpl
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|||||||
@@ -11,29 +11,6 @@ public inline fun String.matches(regex : String) : Boolean {
|
|||||||
return result != null && result.size > 0
|
return result != null && result.size > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public inline fun String.length(): Int = length
|
|
||||||
|
|
||||||
inline val String.size : Int
|
|
||||||
get() = length
|
|
||||||
|
|
||||||
public inline fun String.startsWith(ch: Char): Boolean {
|
|
||||||
return if (size > 0) charAt(0) == ch else false
|
|
||||||
}
|
|
||||||
|
|
||||||
public inline fun String.endsWith(ch: Char): Boolean {
|
|
||||||
val s = size
|
|
||||||
return if (s > 0) charAt(s - 1) == ch else false
|
|
||||||
}
|
|
||||||
|
|
||||||
native
|
|
||||||
fun String.startsWith(s:String):Boolean = noImpl
|
|
||||||
|
|
||||||
native
|
|
||||||
fun String.endsWith(s:String):Boolean = noImpl
|
|
||||||
|
|
||||||
native
|
|
||||||
fun String.contains(s:String):Boolean = noImpl
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this
|
* Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -84,4 +84,8 @@ public final class StringTest extends AbstractExpressionTest {
|
|||||||
public void testStringSplit() throws Exception {
|
public void testStringSplit() throws Exception {
|
||||||
checkFooBoxIsOk();
|
checkFooBoxIsOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testExtensionMethods() throws Exception {
|
||||||
|
fooBoxTest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,12 +61,6 @@ public final class StandardClasses {
|
|||||||
declareKotlinObject(currentFQName, kotlinName);
|
declareKotlinObject(currentFQName, kotlinName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Builder kotlinFunction(@NotNull String kotlinName) {
|
|
||||||
kotlinTopLevelObject(kotlinName);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Builder constructor() {
|
private Builder constructor() {
|
||||||
assert currentFQName != null;
|
assert currentFQName != null;
|
||||||
@@ -105,8 +99,6 @@ public final class StandardClasses {
|
|||||||
|
|
||||||
standardClasses.declare().forFQ("jet.IntProgression").kotlinClass("NumberProgression")
|
standardClasses.declare().forFQ("jet.IntProgression").kotlinClass("NumberProgression")
|
||||||
.methods("iterator", "contains").properties("start", "end", "increment");
|
.methods("iterator", "contains").properties("start", "end", "increment");
|
||||||
|
|
||||||
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+8
-34
@@ -16,49 +16,23 @@
|
|||||||
|
|
||||||
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
|
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
|
||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsInvocation;
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsNameRef;
|
|
||||||
import com.google.dart.compiler.util.AstUtil;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInFunctionIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
|
||||||
import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInPropertyIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInPropertyIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
|
|
||||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredicate;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
|
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
|
||||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
|
|
||||||
|
|
||||||
public final class StringOperationFIF extends CompositeFIF {
|
public final class StringOperationFIF extends CompositeFIF {
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static final DescriptorPredicate GET_PATTERN = pattern("String.get");
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static final FunctionIntrinsic GET_INTRINSIC = new FunctionIntrinsic() {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public JsExpression apply(@Nullable JsExpression receiver,
|
|
||||||
@NotNull List<JsExpression> arguments,
|
|
||||||
@NotNull TranslationContext context) {
|
|
||||||
assert receiver != null;
|
|
||||||
assert arguments.size() == 1 : "String#get expression must have 1 argument.";
|
|
||||||
//TODO: provide better way
|
|
||||||
JsNameRef charAtReference = AstUtil.newQualifiedNameRef("charAt");
|
|
||||||
setQualifier(charAtReference, receiver);
|
|
||||||
return new JsInvocation(charAtReference, arguments);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = new StringOperationFIF();
|
public static final FunctionIntrinsicFactory INSTANCE = new StringOperationFIF();
|
||||||
|
|
||||||
private StringOperationFIF() {
|
private StringOperationFIF() {
|
||||||
add(GET_PATTERN, GET_INTRINSIC);
|
add(pattern("jet", "String", "get"), new BuiltInFunctionIntrinsic("charAt"));
|
||||||
add(pattern("String.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
|
||||||
add(pattern("CharSequence.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
BuiltInPropertyIntrinsic lengthIntrinsic = new BuiltInPropertyIntrinsic("length");
|
||||||
|
add(pattern("jet", "String", "<get-length>"), lengthIntrinsic);
|
||||||
|
add(pattern("js", "<get-size>").receiverExists(true), lengthIntrinsic);
|
||||||
|
add(pattern("js", "length").receiverExists(true), lengthIntrinsic);
|
||||||
|
add(pattern("jet", "CharSequence", "<get-length>"), lengthIntrinsic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun box(): Boolean {
|
||||||
|
val s = "bar"
|
||||||
|
return s.size == 3 && s.length() == 3 && s.length == 3
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package foo
|
package foo
|
||||||
|
|
||||||
val String.size : Int
|
val String.prop : Int
|
||||||
get() = length
|
get() = length
|
||||||
|
|
||||||
val Int.quadruple : Int
|
val Int.quadruple : Int
|
||||||
@@ -8,9 +8,9 @@ val Int.quadruple : Int
|
|||||||
|
|
||||||
fun box() : Boolean
|
fun box() : Boolean
|
||||||
{
|
{
|
||||||
if ("1".size != 1) return false;
|
if ("1".prop != 1) return false;
|
||||||
if ("11".size != 2) return false;
|
if ("11".prop != 2) return false;
|
||||||
if (("121" + "123").size != 6) return false;
|
if (("121" + "123").prop != 6) return false;
|
||||||
if (1.quadruple != 4) return false;
|
if (1.quadruple != 4) return false;
|
||||||
if (0.quadruple != 0) return false;
|
if (0.quadruple != 0) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.lang.split;
|
|
||||||
import js.*;
|
import js.*;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user