-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| 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) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
|
|
||
| package kotlinx.io | ||
|
|
||
| import kotlinx.io.node.os | ||
|
|
||
| public actual open class IOException : Exception { | ||
| public actual constructor() : super() | ||
|
|
||
|
|
@@ -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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it somehow justified that we use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fzhinkin Does it make sense to check whether
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
| 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" |
| 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" |
| 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" |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
WDYT? The lack of consistency bugs me, and writing "on XXX targets" everywhere feels off.