Add Path.replace_utf8! - #461
Open
dmisiuk wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Path.replace_utf8!, closes #386.Discussed on Zulip — @Anton-4 settled the four open questions and retitled the issue accordingly:
Path.replace_utf8!only.Fileis now just buffered readers, so whole-file operations stay onPath. Nothing added toFile.Str.replace_each.patternrather than the builtin'sdelimiter, which reads oddly for file contents.Implementation
Pure Roc over the existing
read_utf8!/write_utf8!and theStr.replace_eachbuiltin, so there are no host or glue changes.Non-atomic, by decision. This reads the whole file, substitutes, and writes it back, so a failure mid-write can leave the file partially written — exactly as a bare
write_utf8!would. The doc comment states this. A safe temp-file-and-rename variant is a larger change that would also apply towrite_utf8!, so it belongs in its own issue rather than being folded in here.Testing
examples/file-replace.rocdemonstrates the API and doubles as the behavioral test: it writes"Hello, World! Hello, Roc!", callsreplace_utf8!("Hello", "Goodbye"), and reads it back. Itstest_spec.jsoncase asserts the exact resultAfter replacing: "Goodbye, World! Goodbye, Roc!", which proves the replace-each behavior — both occurrences change, not just the first.The assertion was verified to fail when the expected string is altered, so it isn't vacuously green. Full
./scripts/test.pypasses — host build plus all examples across every target.replace_utf8!is effectful, so there's no pure core to cover withexpectblocks; the executed example is the proportionate coverage.