Skip to content

Expose a platform-specific line separator #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/api/kotlinx-io-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public abstract interface annotation class kotlinx/io/InternalIoApi : java/lang/
public final class kotlinx/io/JvmCoreKt {
public static final fun asSink (Ljava/io/OutputStream;)Lkotlinx/io/RawSink;
public static final fun asSource (Ljava/io/InputStream;)Lkotlinx/io/RawSource;
public static final fun getSystemLineSeparator ()Ljava/lang/String;
}

public abstract interface class kotlinx/io/RawSink : java/io/Flushable, java/lang/AutoCloseable {
Expand Down
2 changes: 2 additions & 0 deletions core/api/kotlinx-io-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ final val kotlinx.io.unsafe/SegmentReadContextImpl // kotlinx.io.unsafe/SegmentR
final fun <get-SegmentReadContextImpl>(): kotlinx.io.unsafe/SegmentReadContext // kotlinx.io.unsafe/SegmentReadContextImpl.<get-SegmentReadContextImpl>|<get-SegmentReadContextImpl>(){}[0]
final val kotlinx.io.unsafe/SegmentWriteContextImpl // kotlinx.io.unsafe/SegmentWriteContextImpl|{}SegmentWriteContextImpl[0]
final fun <get-SegmentWriteContextImpl>(): kotlinx.io.unsafe/SegmentWriteContext // kotlinx.io.unsafe/SegmentWriteContextImpl.<get-SegmentWriteContextImpl>|<get-SegmentWriteContextImpl>(){}[0]
final val kotlinx.io/SystemLineSeparator // kotlinx.io/SystemLineSeparator|{}SystemLineSeparator[0]
final fun <get-SystemLineSeparator>(): kotlin/String // kotlinx.io/SystemLineSeparator.<get-SystemLineSeparator>|<get-SystemLineSeparator>(){}[0]

final fun (kotlinx.io.files/Path).kotlinx.io.files/sink(): kotlinx.io/Sink // kotlinx.io.files/sink|sink@kotlinx.io.files.Path(){}[0]
final fun (kotlinx.io.files/Path).kotlinx.io.files/source(): kotlinx.io/Source // kotlinx.io.files/source|source@kotlinx.io.files.Path(){}[0]
Expand Down
13 changes: 13 additions & 0 deletions core/apple/src/-PlatformApple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* The value of this property is always `"\n"`.
*/
public actual val SystemLineSeparator: String = "\n"
9 changes: 9 additions & 0 deletions core/common/src/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ private class DiscardingSink : RawSink {
override fun flush() {}
override fun close() {}
}

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* The value of this property is platform-specific, but usually it is either `"\n"` or `"\r\n"`.
*
* See the documentation for a particular platform to get more information about the value of this property.
*/
public expect val SystemLineSeparator: String
21 changes: 21 additions & 0 deletions core/common/test/LineSeparatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io

import kotlinx.io.files.isWindows
import kotlin.test.Test
import kotlin.test.assertEquals

class LineSeparatorTest {
@Test
public fun testLineSeparator() {
if (isWindows) {
assertEquals("\r\n", SystemLineSeparator)
} else {
assertEquals("\n", SystemLineSeparator)
}
}
}
16 changes: 16 additions & 0 deletions core/js/src/-PlatformJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package kotlinx.io

import kotlinx.io.node.os

public actual open class IOException : Exception {
public actual constructor() : super()

Expand All @@ -31,3 +33,17 @@ internal actual fun withCaughtException(block: () -> Unit): Throwable? {
return t
}
}

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* In NodeJS-compatible environments, this property derives value from [os.EOL](https://nodejs.org/api/os.html#oseol),
* in all other environments (like a web-browser), its value is always `"\n"`.
*/
public actual val SystemLineSeparator: String by lazy {
try {
os.EOL
} catch (_: Throwable) {
"\r\n"
}
}
7 changes: 7 additions & 0 deletions core/jvm/src/JvmCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,10 @@ internal val AssertionError.isAndroidGetsocknameError: Boolean
get() {
return cause != null && message?.contains("getsockname failed") ?: false
}

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* Returns the same value as [System.lineSeparator].
*/
public actual val SystemLineSeparator: String = System.lineSeparator()
13 changes: 13 additions & 0 deletions core/mingw/src/-PlatformMingw.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* The value of this property is always `"\r\n"`.
*/
public actual val SystemLineSeparator: String = "\r\n"
5 changes: 5 additions & 0 deletions core/nodeFilesystemShared/src/node/os.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ internal external interface Os {
* See https://nodejs.org/api/os.html#osplatform
*/
fun platform(): String

/**
* See https://nodejs.org/api/os.html#oseol
*/
val EOL: String
}

internal expect val os: Os
13 changes: 13 additions & 0 deletions core/unix/src/-PlatformUnix.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* The value of this property is always `"\n"`.
*/
public actual val SystemLineSeparator: String = "\n"
15 changes: 15 additions & 0 deletions core/wasmJs/src/-PlatformWasmJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package kotlinx.io

import kotlinx.io.node.os

internal class JsException(message: String) : RuntimeException(message)

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

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* In NodeJS-compatible environments, this property derives value from [os.EOL](https://nodejs.org/api/os.html#oseol),
* in all other environments (like a web-browser), its value is always `"\n"`.
*/
public actual val SystemLineSeparator: String by lazy {
try {
os.EOL
} catch (_: Throwable) {
"\n"
}
}
13 changes: 13 additions & 0 deletions core/wasmWasi/src/-PlatformWasmWasi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2025 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io

/**
* Sequence of characters used as a line separator by the underlying platform.
*
* The value of this property is always `"\n"`.
*/
public actual val SystemLineSeparator: String = "\n"