Code Monkey home page Code Monkey logo

Comments (5)

utkarshkukreti avatar utkarshkukreti commented on August 9, 2024

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.

Lisoph avatar Lisoph commented on August 9, 2024

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.

utkarshkukreti avatar utkarshkukreti commented on August 9, 2024

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.

Lisoph avatar Lisoph commented on August 9, 2024

@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.

utkarshkukreti avatar utkarshkukreti commented on August 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.