Skip to content

Commit d878fa0

Browse files
committed
Expose a platform-specific line separator
Closes #448
1 parent 6bd6a46 commit d878fa0

File tree

12 files changed

+128
-0
lines changed

12 files changed

+128
-0
lines changed

core/api/kotlinx-io-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public abstract interface annotation class kotlinx/io/InternalIoApi : java/lang/
9191
public final class kotlinx/io/JvmCoreKt {
9292
public static final fun asSink (Ljava/io/OutputStream;)Lkotlinx/io/RawSink;
9393
public static final fun asSource (Ljava/io/InputStream;)Lkotlinx/io/RawSource;
94+
public static final fun getSystemLineSeparator ()Ljava/lang/String;
9495
}
9596

9697
public abstract interface class kotlinx/io/RawSink : java/io/Flushable, java/lang/AutoCloseable {

core/api/kotlinx-io-core.klib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ final val kotlinx.io.unsafe/SegmentReadContextImpl // kotlinx.io.unsafe/SegmentR
233233
final fun <get-SegmentReadContextImpl>(): kotlinx.io.unsafe/SegmentReadContext // kotlinx.io.unsafe/SegmentReadContextImpl.<get-SegmentReadContextImpl>|<get-SegmentReadContextImpl>(){}[0]
234234
final val kotlinx.io.unsafe/SegmentWriteContextImpl // kotlinx.io.unsafe/SegmentWriteContextImpl|{}SegmentWriteContextImpl[0]
235235
final fun <get-SegmentWriteContextImpl>(): kotlinx.io.unsafe/SegmentWriteContext // kotlinx.io.unsafe/SegmentWriteContextImpl.<get-SegmentWriteContextImpl>|<get-SegmentWriteContextImpl>(){}[0]
236+
final val kotlinx.io/SystemLineSeparator // kotlinx.io/SystemLineSeparator|{}SystemLineSeparator[0]
237+
final fun <get-SystemLineSeparator>(): kotlin/String // kotlinx.io/SystemLineSeparator.<get-SystemLineSeparator>|<get-SystemLineSeparator>(){}[0]
236238

237239
final fun (kotlinx.io.files/Path).kotlinx.io.files/sink(): kotlinx.io/Sink // kotlinx.io.files/sink|[email protected](){}[0]
238240
final fun (kotlinx.io.files/Path).kotlinx.io.files/source(): kotlinx.io/Source // kotlinx.io.files/source|[email protected](){}[0]

core/apple/src/-PlatformApple.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io
7+
8+
/**
9+
* Sequence of characters used as a line separator by the underlying platform.
10+
*
11+
* The value of this property is always `"\n"`.
12+
*/
13+
public actual val SystemLineSeparator: String = "\n"

core/common/src/Core.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,12 @@ private class DiscardingSink : RawSink {
4343
override fun flush() {}
4444
override fun close() {}
4545
}
46+
47+
/**
48+
* Sequence of characters used as a line separator by the underlying platform.
49+
*
50+
* The value of this property is platform-specific, but usually it is either `"\n"` or `"\r\n"`.
51+
*
52+
* See the documentation for a particular platform to get more information about the value of this property.
53+
*/
54+
public expect val SystemLineSeparator: String

core/common/test/LineSeparatorTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io
7+
8+
import kotlinx.io.files.isWindows
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
class LineSeparatorTest {
13+
@Test
14+
public fun testLineSeparator() {
15+
if (isWindows) {
16+
assertEquals("\r\n", SystemLineSeparator)
17+
} else {
18+
assertEquals("\n", SystemLineSeparator)
19+
}
20+
}
21+
}

core/js/src/-PlatformJs.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package kotlinx.io
77

8+
import kotlinx.io.node.os
9+
810
public actual open class IOException : Exception {
911
public actual constructor() : super()
1012

@@ -31,3 +33,17 @@ internal actual fun withCaughtException(block: () -> Unit): Throwable? {
3133
return t
3234
}
3335
}
36+
37+
/**
38+
* Sequence of characters used as a line separator by the underlying platform.
39+
*
40+
* In NodeJS-compatible environments, this property derives value from [os.EOL](https://nodejs.org/api/os.html#oseol),
41+
* in all other environments (like a web-browser), its value is always `"\n"`.
42+
*/
43+
public actual val SystemLineSeparator: String by lazy {
44+
try {
45+
os.EOL
46+
} catch (_: Throwable) {
47+
"\r\n"
48+
}
49+
}

core/jvm/src/JvmCore.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ internal val AssertionError.isAndroidGetsocknameError: Boolean
109109
get() {
110110
return cause != null && message?.contains("getsockname failed") ?: false
111111
}
112+
113+
/**
114+
* Sequence of characters used as a line separator by the underlying platform.
115+
*
116+
* Returns the same value as [System.lineSeparator].
117+
*/
118+
public actual val SystemLineSeparator: String = System.lineSeparator()

core/mingw/src/-PlatformMingw.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io
7+
8+
/**
9+
* Sequence of characters used as a line separator by the underlying platform.
10+
*
11+
* The value of this property is always `"\r\n"`.
12+
*/
13+
public actual val SystemLineSeparator: String = "\r\n"

core/nodeFilesystemShared/src/node/os.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ internal external interface Os {
1616
* See https://nodejs.org/api/os.html#osplatform
1717
*/
1818
fun platform(): String
19+
20+
/**
21+
* See https://nodejs.org/api/os.html#oseol
22+
*/
23+
val EOL: String
1924
}
2025

2126
internal expect val os: Os

core/unix/src/-PlatformUnix.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io
7+
8+
/**
9+
* Sequence of characters used as a line separator by the underlying platform.
10+
*
11+
* The value of this property is always `"\n"`.
12+
*/
13+
public actual val SystemLineSeparator: String = "\n"

core/wasmJs/src/-PlatformWasmJs.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package kotlinx.io
77

8+
import kotlinx.io.node.os
9+
810
internal class JsException(message: String) : RuntimeException(message)
911

1012
internal actual fun withCaughtException(block: () -> Unit): Throwable? {
@@ -23,3 +25,16 @@ private fun catchJsThrowable(block: () -> Unit): JsAny? = js("""{
2325
}
2426
}""")
2527

28+
/**
29+
* Sequence of characters used as a line separator by the underlying platform.
30+
*
31+
* In NodeJS-compatible environments, this property derives value from [os.EOL](https://nodejs.org/api/os.html#oseol),
32+
* in all other environments (like a web-browser), its value is always `"\n"`.
33+
*/
34+
public actual val SystemLineSeparator: String by lazy {
35+
try {
36+
os.EOL
37+
} catch (_: Throwable) {
38+
"\n"
39+
}
40+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
package kotlinx.io
7+
8+
/**
9+
* Sequence of characters used as a line separator by the underlying platform.
10+
*
11+
* The value of this property is always `"\n"`.
12+
*/
13+
public actual val SystemLineSeparator: String = "\n"

0 commit comments

Comments
 (0)