Skip to content

Commit

Permalink
final fix for #7
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Sep 14, 2023
1 parent 6334d7d commit d9b90d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/samples.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! ```text
//! ├ special characters here
//! ├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├├
//! ```

#![allow(unused)]
Expand Down
21 changes: 14 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens};
use regex::Regex;
use std::{
cmp::min,
collections::HashMap,
fs::{self, OpenOptions},
io::Write,
Expand Down Expand Up @@ -554,7 +555,7 @@ impl SourceEntity {
/// Marks the character positions corresponding with this entity as belonging to this
/// entity in the enclosing [`CompressedString`].
pub fn claim(&self, claimed: &mut Vec<bool>) {
for i in self.start..(self.end + 1) {
for i in self.start..min(self.end, claimed.len()) {
claimed[i] = true;
}
}
Expand Down Expand Up @@ -601,7 +602,7 @@ static MARKDOWN_CODEBLOCK: Lazy<Regex> = Lazy::new(|| Regex::new(r"```[\s\S]*?``
impl From<&String> for CompressedString {
fn from(value: &String) -> Self {
let mut entities: Vec<SourceEntity> = Vec::new();
let mut claimed: Vec<bool> = value.chars().map(|_| false).collect();
let mut claimed: Vec<bool> = vec![false; value.len()];
for m in DOC_COMMENT.find_iter(value) {
let entity = SourceEntity::new(m.start(), m.end());
entity.claim(&mut claimed);
Expand Down Expand Up @@ -639,17 +640,23 @@ impl From<&String> for CompressedString {
chars_arr: Vec::new(),
chars: HashMap::new(),
};
let chars: Vec<char> = value.chars().collect();
let mut cursor = 0;
for (i, c) in chars.iter().enumerate() {
if claimed[i] || c.is_whitespace() {
let mut byte_index = 0;
while byte_index < value.len() {
let current_char = &value[byte_index..].chars().next().unwrap(); // get the current character
let char_len = current_char.len_utf8(); // get its length in bytes

if claimed[byte_index] || current_char.is_whitespace() {
byte_index += char_len;
continue;
}
let oc = OffsetChar::new(*c, i);
let oc = OffsetChar::new(*current_char, byte_index);
compressed.chars.insert(cursor, oc);
compressed.chars_arr.push(oc);
cursor += 1;
byte_index += char_len;
}

compressed
}
}
Expand Down Expand Up @@ -677,7 +684,7 @@ fn source_excerpt<'a>(source: &'a String, item: &'a Item) -> Result<String> {
let start_pos = line_start_position(source, start_pos);
let end_c = compressed_source.chars[&(found_start + compressed_item_string.len() - 1)];
let end_pos = end_c.original_pos;
let final_excerpt = &source[(start_pos)..(end_pos + 1)];
let final_excerpt = &source[start_pos..min(end_pos + 1, source.len())];
Ok(final_excerpt
.lines()
.map(|line| {
Expand Down

0 comments on commit d9b90d1

Please sign in to comment.