Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request : #3321 #3846 #3850 #3857

Closed
wants to merge 0 commits into from
Closed

Conversation

rustbasic
Copy link
Contributor

@rustbasic rustbasic commented Jan 22, 2024

@emilk
Dear emilk.

I work finished, Pull Request.

Changed :
crates/eframe/src/native/run.rs

The test results are normal.

rev() is panic with UTF8

Not byte_index
It's char_index ( for UTF8 )

I want to use the following for accurate usage:
egui\src\text_selection\text_cursor_state.rs 360 lines
let width = unicode_width::UnicodeWidthChar::width_cjk(c).unwrap_or(2);

Thank you, emilk.

@rustbasic
Copy link
Contributor Author

@emilk
Dear emilk,

I'm having trouble with cargo check errors while making a Pull Request,
and I'm not sure why.
If there's nothing unusual in the source code,
please either merge it as is or feel free to make modifications as you see fit.

I've extensively tested it in the program I created,
so I believe there shouldn't be any bugs.

Thank you, emilk.

@emilk
Copy link
Owner

emilk commented Jan 23, 2024

please create one PR per issue to make it easier to review

None => CCursor::new(0),
let mut count = 0;
for (_i, c) in text.chars().enumerate() {
count += 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use the i from the enumerate? Or just remove the enumerate() since it is not doing anything. Or do this:

    let line_indexes: Vec<_> = text
        .lines()
        .map(|line| line.chars().count())
        .scan(0, |total, next| {
            let index = *total;
            *total += next;
            Some(index)
        })
        .collect();

Is the vec supposed to always contain the last char index as well? Even if it is not a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'\r' and '\n' should also be counted.
Saving the last index also reduces panic or bug occurrence when using arrays.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it introduce bugs, though? You are saving the last index of the last line as if it were the first index of a new line.


pub fn get_line_and_start_index(line_indexes: &mut [usize], index: usize) -> (usize, usize) {
let mut current_line = 1;
for (i, line_start) in line_indexes.iter().enumerate() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use partition_point.

let mut i: usize = line_start_index;
let mut current_column: usize = 1;
let mut total_columns: usize = 0;
while let Some(c) = text.chars().nth(i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has quadratic complexity, doesn't it? Each call to nth() is a for loop from the start of the string. You could do

for c in text.chars().skip(line_start_index) {

Since you are already breaking on the newline. Otherwise you can get the index of the next line start and use take() to get the chars up to that index.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to find a column according to the width of UTF8 characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants