Comments (5)
Do you have any suggestions on how the API for this would look like? Also, with the current API, you can just declare 2 mutable variables for the indices and increment them when appropriate:
let mut li = 0;
let mut ri = 0;
for diff in diff::lines(...) {
match diff {
Result::Left(_) => { ...; li += 1; },
Result::Both(_, _) => { ...; li += 1; ri += 1; },
Result::Right(_) => { ...; ri += 1; }
}
}
from diff.rs.
Hi @utkarshkukreti, thanks for the suggestion.
Yep I tried the approach you showed, but sadly it doesn't work.
Take the following code as an example:
let mut cur_line = 0;
for diff in diff::lines(&orig, &new) {
match diff {
diff::Result::Right(_) => println!("+{}", cur_line),
_ => {}
}
cur_line += 1;
}
With orig
being:
line
line
line
line
line
line
line
line
line
line
and new
being:
line
line
line; change 1
line
line
line
line
line; change 2
line
line
the program yields +2 +9
which are the zero-based indices.
The 2
is correct (change 1
), yet the 9
is not (2 lines below change 2
).
What I'm trying to achieve is basically iterating through the Right(_)
results and determining the line number corresponding to that difference.
The problem is that incrementing a counter does not semantically correspond to the "real indices". In other words, it does not track which line, but how many lines, which is not the same.
API wise an idea might be to implement a function similar to the CharIndices iterator in the Rust std.
It might also not be a bad idea to make the entire library iterator based. This would make the lib more flexible to use and also makes it more idiomatic (prefer returning a lazily evaluating iterator over a Vec).
Of course the matrix / table still needs to be built immediatly (I think), but tracing it can be done through an iterator.
What we would end up with ideally would be something like diff::lines(...)
, which yields for example a diff::Result::Left(l)
, and a diff::line_indices(...)
, which could yield a diff::IndexResult::Left(i, l)
.
The names are of course debatable.
I have some implementation concepts in mind. I'm going to submit a pull request soon, if that's ok with you.
Cheers!
from diff.rs.
Take the following code as an example:
I think that's easy to fix. If you increment the right index only in Both
and Right
(like my first comment here):
extern crate diff;
fn main() {
let orig = "line
line
line
line
line
line
line
line
line
line";
let new = "line
line
line; change 1
line
line
line
line
line; change 2
line
line";
let mut ri = 0;
for diff in diff::lines(&orig, &new) {
match diff {
diff::Result::Right(_) => {
println!("+{}", ri);
ri += 1;
}
diff::Result::Both(_, _) => {
ri += 1;
}
_ => {}
}
}
}
you get:
+2
+7
That's the expected output right?
from diff.rs.
@utkarshkukreti seems like you are right. I could swear I tried exactly this approach before I opened the issue and it produced all wrong numbers. Must have been a silly mistake I guess.
I'm going to test this properly and let you know if it works.
from diff.rs.
Closing as it worked for me and you didn't reply after that but please comment if it didn't work for you!
from diff.rs.
Related Issues (10)
- Implement Myers' Diff algorithm HOT 1
- Line by line comparison doesn't include last empty line HOT 1
- Is this crate maintained? HOT 1
- Performance issue with the redundancy load HOT 5
- Why does `Result::Both` contain two values? HOT 1
- Big allocation for a small diff HOT 2
- In case of a mismatch, insert Left entries before Right
- Relicense under dual MIT/Apache-2.0 HOT 3
- please include LICENSE text into archive HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from diff.rs.