Skip to content

Commit 0d88737

Browse files
committed
Merge pull request #99542 from bruvzg/stdin_str
Convert line breaks to `\n` and strip line break from the end of string returned by `OS::read_string_from_stdin`/`OS::get_stdin_string`.
2 parents 6e6fbdd + e9b57fc commit 0d88737

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

doc/classes/OS.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@
721721
- If standard input is console, this method will block until the program receives a line break in standard input (usually by the user pressing [kbd]Enter[/kbd]).
722722
- If standard input is pipe, this method will block until a specific amount of data is read or pipe is closed.
723723
- If standard input is a file, this method will read a specific amount of data (or less if end-of-file is reached) and return immediately.
724+
[b]Note:[/b] This method automatically replaces [code]\r\n[/code] line breaks with [code]\n[/code] and removes them from the end of the string. Use [method read_buffer_from_stdin] to read the unprocessed data.
724725
[b]Note:[/b] This method is implemented on Linux, macOS, and Windows.
725726
[b]Note:[/b] On exported Windows builds, run the console wrapper executable to access the terminal. If standard input is console, calling this method without console wrapped will freeze permanently. If standard input is pipe or file, it can be used without console wrapper. If you need a single executable with full console support, use a custom build compiled with the [code]windows_subsystem=console[/code] flag.
726727
</description>

drivers/unix/os_unix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ String OS_Unix::get_stdin_string(int64_t p_buffer_size) {
191191
Vector<uint8_t> data;
192192
data.resize(p_buffer_size);
193193
if (fgets((char *)data.ptrw(), data.size(), stdin)) {
194-
return String::utf8((char *)data.ptr());
194+
return String::utf8((char *)data.ptr()).replace("\r\n", "\n").rstrip("\n");
195195
}
196196
return String();
197197
}

platform/windows/os_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ String OS_Windows::get_stdin_string(int64_t p_buffer_size) {
17391739
data.resize(p_buffer_size);
17401740
DWORD count = 0;
17411741
if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), data.ptrw(), data.size(), &count, nullptr)) {
1742-
return String::utf8((const char *)data.ptr(), count);
1742+
return String::utf8((const char *)data.ptr(), count).replace("\r\n", "\n").rstrip("\n");
17431743
}
17441744

17451745
return String();

0 commit comments

Comments
 (0)