Skip to content
Open
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to explicitly write about it in a specific platform, like:
Sequence of characters used as a line separator in Apple targets?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer having the same brief kdoc line across all actualizations, and only add platform specific details in the expanded version of a doc. But that's a personal preference, I don't have strong arguments to support it (docs will be different anyway). So we can try your suggestion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the target, I tend to end the phrase "Sequence of characters used as a line separator ... " with:

  • "... in JS environments"
  • "... by Windows"
  • "... on Apple targets"
  • "... by Unix platforms"
  • "... by the JVM"

WDYT? The lack of consistency bugs me, and writing "on XXX targets" everywhere feels off.

*
* 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it somehow justified that we use \r\n as a fallback?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML uses CRLF as a line separator, so I thought it's a good idea to fall back to it. But it has to be explicitly specified.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait, it's already documented and says that the separator is different :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JSMonk does such a choice make any sense? Or it's better to use \n?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fzhinkin Does it make sense to check whether navigator.platform is Windows, and if so, use \r\n; otherwise use \n for Android, Linux, macOS, and pretty much every other Os?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at this from the following perspective: a browser and JS code executed inside is aimed for interaction with Internet, and the line separator used for documents shared and transferred there is somewhat unrelated to the operating system where the browser is running. That's why I picked the line separator from HTTP standard.

But IDK how far fetched it is from the actual use cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Browsers have access to the file system now, and likely should represent the OS on which they're running.

}
}
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"