Add String.subSequence implementation. (#61)

This commit is contained in:
Nikolay Igotti
2016-11-15 13:27:21 +03:00
committed by GitHub
parent 69b7cdb866
commit c5cfa0ac4f
5 changed files with 34 additions and 4 deletions
+5
View File
@@ -198,6 +198,11 @@ task tostring0(type: RunKonanTest) {
source = "runtime/basic/tostring0.kt" source = "runtime/basic/tostring0.kt"
} }
task tostring1(type: RunKonanTest) {
goldValue = "ello\n"
source = "runtime/basic/tostring1.kt"
}
task array0(type: RunKonanTest) { task array0(type: RunKonanTest) {
goldValue = "5\n6\n7\n8\n9\n10\n11\n12\n13\n" goldValue = "5\n6\n7\n8\n9\n10\n11\n12\n13\n"
source = "runtime/basic/array0.kt" source = "runtime/basic/array0.kt"
@@ -0,0 +1,4 @@
fun main(args : Array<String>) {
val hello = "Hello world"
println(hello.subSequence(1, 5).toString())
}
+18 -3
View File
@@ -11,6 +11,9 @@
extern "C" { extern "C" {
// TODO: remove, once can generate empty string constant in compile time.
KString theEmptyString = nullptr;
// Any.kt // Any.kt
KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other) { KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other) {
return thiz == other; return thiz == other;
@@ -123,9 +126,21 @@ KInt Kotlin_String_hashCode(KString thiz) {
return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_); return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_);
} }
KRef Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex) { KString Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex) {
RuntimeAssert(false, "Unsupported operation"); if (startIndex < 0 || endIndex >= thiz->count_ || startIndex > endIndex) {
return nullptr; // TODO: is it correct exception?
ThrowArrayIndexOutOfBoundsException();
}
if (startIndex == endIndex) {
return theEmptyString;
}
// TODO: support UTF-8.
KInt length = endIndex - startIndex;
ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace();
memcpy(ByteArrayAddressOfElementAt(result, 0),
ByteArrayAddressOfElementAt(thiz, startIndex),
length);
return result;
} }
} // extern "C" } // extern "C"
+3 -1
View File
@@ -51,6 +51,8 @@ inline const KRef* ArrayAddressOfElementAt(const ArrayHeader* obj, KInt index) {
extern "C" { extern "C" {
#endif #endif
extern KString theEmptyString;
// Any.kt // Any.kt
KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other); KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other);
KInt Kotlin_Any_hashCode(KConstRef thiz); KInt Kotlin_Any_hashCode(KConstRef thiz);
@@ -95,7 +97,7 @@ KChar Kotlin_String_get(KString thiz, KInt index);
KString Kotlin_String_fromUtf8Array(const ArrayHeader* array); KString Kotlin_String_fromUtf8Array(const ArrayHeader* array);
KString Kotlin_String_plusImpl(KString thiz, KString other); KString Kotlin_String_plusImpl(KString thiz, KString other);
KInt Kotlin_String_getStringLength(KString thiz); KInt Kotlin_String_getStringLength(KString thiz);
KRef Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex); KString Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex);
#ifdef __cplusplus #ifdef __cplusplus
} }
+4
View File
@@ -3,6 +3,10 @@ package kotlin
@SymbolName("Kotlin_String_fromUtf8Array") @SymbolName("Kotlin_String_fromUtf8Array")
external fun fromUtf8Array(array: ByteArray) : String external fun fromUtf8Array(array: ByteArray) : String
// TODO: enable, once global variables implemented.
// @SymbolName("theEmptyString")
// val theEmptyString = ""
@ExportTypeInfo("theStringTypeInfo") @ExportTypeInfo("theStringTypeInfo")
public final class String : Comparable<String>, CharSequence { public final class String : Comparable<String>, CharSequence {
@SymbolName("Kotlin_String_hashCode") @SymbolName("Kotlin_String_hashCode")