Make wordcount filter work with core only#480
Conversation
|
The Can you adapt ↓ to test if the changed code works well with the partial inputs, too? askama/askama/src/filters/indent.rs Lines 320 to 331 in c40e828 |
246b68a to
a575431
Compare
|
Renamed |
| if s.is_empty() { | ||
| return Ok(()); | ||
| } else if s.trim().is_empty() { | ||
| self.0.ends_with_whitespace = true; | ||
| return Ok(()); | ||
| } | ||
| self.0.count += s.split_whitespace().count(); | ||
| if !self.0.ends_with_whitespace && !s.starts_with(char::is_whitespace) { | ||
| self.0.count -= 1; | ||
| } | ||
| self.0.ends_with_whitespace = s.ends_with(char::is_whitespace); | ||
| Ok(()) |
There was a problem hiding this comment.
I had a bit of a problem understanding what was going on, so I refactored the code a bit:
| if s.is_empty() { | |
| return Ok(()); | |
| } else if s.trim().is_empty() { | |
| self.0.ends_with_whitespace = true; | |
| return Ok(()); | |
| } | |
| self.0.count += s.split_whitespace().count(); | |
| if !self.0.ends_with_whitespace && !s.starts_with(char::is_whitespace) { | |
| self.0.count -= 1; | |
| } | |
| self.0.ends_with_whitespace = s.ends_with(char::is_whitespace); | |
| Ok(()) | |
| if s.is_empty() { | |
| return Ok(()); | |
| } | |
| // first cut of all whitespaces from the front | |
| let trimmed_s = s.trim_start(); | |
| let started_with_withspace = trimmed_s.len() != s.len(); | |
| let s = trimmed_s; | |
| if s.is_empty() { | |
| // there was nothing but space in the input | |
| self.0.ends_with_whitespace = true; | |
| return Ok(()); | |
| } | |
| let space_in_between = started_with_withspace || self.0.ends_with_whitespace; | |
| // then cut of all whitespaces from the back | |
| let trimmed_s = s.trim_end(); | |
| self.0.ends_with_whitespace = trimmed_s.len() != s.len(); | |
| let s = trimmed_s; | |
| // `s` cannot be empty. Otherwise `s.trim_start()` would have been empty. | |
| // split at whitespaces and decrement word count if there was no space in between the calls | |
| self.0.count += s.split_whitespace().count(); | |
| if !space_in_between { | |
| self.0.count -= 1; | |
| } | |
| Ok(()) |
The logic is the same, but in many more lines. You decide if it's an improvement. :)
There was a problem hiding this comment.
Not a big fan but definitely gonna add more code comments.
a575431 to
35483d0
Compare
|
I added code comments. Please tell me if:
|
Kijewski
left a comment
There was a problem hiding this comment.
Looks good. The comments are understandable to me. :) Thank you!
Part of #472.