Skip to content

Commit cc494ec

Browse files
committed
update function's doc comment
1 parent 39d5465 commit cc494ec

File tree

1 file changed

+19
-8
lines changed
  • cpp-linter/src/common_fs

1 file changed

+19
-8
lines changed

Diff for: cpp-linter/src/common_fs/mod.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,15 @@ impl FileObj {
219219
}
220220
}
221221

222-
/// Gets the line and column number from a given `offset` (of bytes) for given
223-
/// `file_path`.
222+
/// Gets the line number for a given `offset` (of bytes) from the given
223+
/// buffer `contents`.
224224
///
225-
/// This computes the line and column numbers from a buffer of bytes read from the
226-
/// `file_path`. In non-UTF-8 encoded files, this does not guarantee that a word
227-
/// boundary exists at the returned column number. However, the `offset` given to this
228-
/// function is expected to originate from diagnostic information provided by
229-
/// clang-format or clang-tidy.
225+
/// The `offset` given to this function is expected to originate from
226+
/// diagnostic information provided by clang-format. Any `offset` out of
227+
/// bounds is clamped to the given `contents` buffer's length.
230228
pub fn get_line_count_from_offset(contents: &[u8], offset: u32) -> u32 {
231229
let offset = (offset as usize).min(contents.len());
232230
let lines = contents[0..offset].split(|byte| byte == &b'\n');
233-
234231
lines.count() as u32
235232
}
236233

@@ -316,6 +313,20 @@ mod test {
316313
assert_eq!(lines, 13);
317314
}
318315

316+
#[test]
317+
fn get_line_count_edge_cases() {
318+
// Empty content
319+
assert_eq!(get_line_count_from_offset(&[], 0), 1);
320+
321+
// No newlines
322+
assert_eq!(get_line_count_from_offset(b"abc", 3), 1);
323+
324+
// Consecutive newlines
325+
assert_eq!(get_line_count_from_offset(b"a\n\nb", 3), 3);
326+
327+
// Offset beyond content length
328+
assert_eq!(get_line_count_from_offset(b"a\nb\n", 10), 3);
329+
}
319330
// *********************** tests for FileObj::get_ranges()
320331

321332
#[test]

0 commit comments

Comments
 (0)