Skip to content
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/oxfmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cow-utils = { workspace = true }
ignore = { workspace = true, features = ["simd-accel"] }
miette = { workspace = true }
rayon = { workspace = true }
simdutf8 = { workspace = true }
tracing-subscriber = { workspace = true, features = [] } # Omit the `regex` feature

# NAPI dependencies (conditional on napi feature)
Expand Down
18 changes: 16 additions & 2 deletions apps/oxfmt/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::Path, sync::mpsc, time::Instant};
use std::{fs, io, path::Path, sync::mpsc, time::Instant};

use cow_utils::CowUtils;
use rayon::prelude::*;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl FormatService {
let source_type = enable_jsx_source_type(entry.source_type);

let allocator = self.allocator_pool.get();
let source_text = fs::read_to_string(path).expect("Failed to read file");
let source_text = read_to_string(path).expect("Failed to read file");

let ret = Parser::new(&allocator, &source_text, source_type)
.with_options(get_parse_options())
Expand Down Expand Up @@ -148,3 +148,17 @@ impl FormatService {
}
}
}

fn read_to_string(path: &Path) -> io::Result<String> {
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
let bytes = fs::read(path)?;
if simdutf8::basic::from_utf8(&bytes).is_err() {
// Same error as `fs::read_to_string` produces (using `io::ErrorKind::InvalidData`)
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
));
}
// SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
Ok(unsafe { String::from_utf8_unchecked(bytes) })
}
Loading