{"id":16856410,"url":"https://github.com/vallentin/line-span","last_synced_at":"2025-03-22T06:31:07.719Z","repository":{"id":43404064,"uuid":"239503197","full_name":"vallentin/line-span","owner":"vallentin","description":"Find line ranges and jump between next and previous lines","archived":false,"fork":false,"pushed_at":"2023-06-12T23:31:55.000Z","size":43,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T08:53:36.842Z","etag":null,"topics":["line","lines","rust","scan","scanner"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vallentin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-10T12:10:45.000Z","updated_at":"2024-07-24T23:53:23.000Z","dependencies_parsed_at":"2024-10-28T12:27:53.495Z","dependency_job_id":"7c9e1690-8b8b-4b23-8238-ff3583e4cded","html_url":"https://github.com/vallentin/line-span","commit_stats":{"total_commits":20,"total_committers":3,"mean_commits":6.666666666666667,"dds":0.09999999999999998,"last_synced_commit":"3ad06b5234f9257454c887f8b701d38c5aa3325f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fline-span","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fline-span/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fline-span/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fline-span/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vallentin","download_url":"https://codeload.github.com/vallentin/line-span/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244918500,"owners_count":20531682,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["line","lines","rust","scan","scanner"],"created_at":"2024-10-13T14:04:09.322Z","updated_at":"2025-03-22T06:31:07.081Z","avatar_url":"https://github.com/vallentin.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# line-span\n\n[![CI](https://github.com/vallentin/line-span/workflows/CI/badge.svg)](https://github.com/vallentin/line-span/actions?query=workflow%3ACI)\n[![Latest Version](https://img.shields.io/crates/v/line-span.svg)](https://crates.io/crates/line-span)\n[![Docs](https://docs.rs/line-span/badge.svg)](https://docs.rs/line-span)\n[![License](https://img.shields.io/github/license/vallentin/line-span.svg)](https://github.com/vallentin/line-span)\n\nThis crate features utilities for finding the start, end, and range of\nlines from a byte index. Further also being able to find the next and\nprevious line, from an arbitrary byte index.\n\nThis is provided via the \u003ccode\u003etrait [LineSpanExt]\u003c/code\u003e, which is\nimplemented for [`str`], and provides the following methods:\n\n## Usage\n\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\nline-span = \"0.1\"\n```\n\n## Releases\n\nRelease notes are available in the repo at [CHANGELOG.md].\n\n-----\n\n**Current Line:**\n\n- [`find_line_start()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_line_start) to find the start of the current line.\n- [`find_line_end()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_line_end) to find the end of the current line.\n- [`find_line_range()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_line_range) to find the start and end current line.\n\n**Next Line:**\n\n- [`find_next_line_start()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_next_line_start) to find the start of the next line.\n- [`find_next_line_end()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_next_line_end) to find the end of the next line.\n- [`find_next_line_range()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_next_line_range) to find the start and end of the next line.\n\n**Previous Line:**\n\n- [`find_prev_line_start()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_prev_line_start) to find the start of the previous line.\n- [`find_prev_line_end()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_prev_line_end) to find the end of the previous line.\n- [`find_prev_line_range()`](https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html#tymethod.find_prev_line_range) to find both start and end of the previous line.\n\n**Utilities:**\n\n- [`str_to_range()`] to get the range of a substring in a string.\n- [`str_to_range_unchecked()`] unchecked version of [`str_to_range()`].\n\n[`str_to_range`]: https://docs.rs/line-span/*/line_span/fn.str_to_range.html\n[`str_to_range_unchecked`]: https://docs.rs/line-span/*/line_span/fn.str_to_range_unchecked.html\n\n## [`LineSpan`] and [`LineSpanIter`]\n\nThe crate includes the [`LineSpanIter`] iterator. It is functionally equivalent to [`str::lines()`],\nwith the addition that it includes the ability to get the start and end byte indices of each line.\nAdditionally, it also includes the ability to get the end including and excluding the line ending (`\\n` or `\\r\\n`).\n\nAn [`LineSpanIter`] can be created by calling [`line_spans()`](https://docs.rs/line-span/*/line_span/trait.LineSpans.html#tymethod.line_spans), implemented in the [`LineSpans`] trait. The crate implements the [`LineSpans`] trait for [`str`] and [`String`].\n\nNote, [`LineSpan`] implements [`Deref`] to [`\u0026str`], so in general,\nswapping [`lines()`] to [`line_spans()`] would not cause many issues.\n\n```rust\nuse line_span::LineSpans;\n\nlet text = \"foo\\nbar\\r\\nbaz\";\n\nfor span in text.line_spans() {\n    println!(\n        \"{:\u003e2?}: {:?} {:?} {:?}\",\n        span.range(),\n        span.as_str(),\n        span.as_str_with_ending(),\n        span.ending_str(),\n    );\n}\n```\n\nThis will output the following:\n\n_(Manually aligned for better readability)_\n\n```text\n0.. 3: \"foo\" \"foo\\n\"   \"\\n\"\n4.. 7: \"bar\" \"bar\\r\\n\" \"\\r\\n\"\n9..12: \"baz\" \"baz\"     \"\"\n```\n\n## Current Line, Previous Line, and Next Line\n\n```rust\nuse line_span::LineSpanExt;\n\nlet text = \"foo\\nbar\\r\\nbaz\";\n//                ^\nlet i = 5; // 'a' in \"bar\"\n\nlet curr_range = text.find_line_range(i);\nlet next_range = text.find_next_line_range(i).unwrap();\nlet prev_range = text.find_prev_line_range(i).unwrap();\n\nassert_eq!(curr_range, 4..7);\nassert_eq!(\u0026text[curr_range], \"bar\");\n\nassert_eq!(prev_range, 0..3);\nassert_eq!(\u0026text[prev_range], \"foo\");\n\nassert_eq!(next_range, 9..12);\nassert_eq!(\u0026text[next_range], \"baz\");\n```\n\n## Range of Substring in String\n\nUse [`str_to_range`] (or [`str_to_range_unchecked`]) to get the\nrange of a substring in a string.\n\n```rust\nlet string1 = \"Foo Bar Baz\";\nlet string2 = \"Hello World\";\n\nlet substring = \u0026string1[4..7]; // \"Bar\"\n\n// Returns `Some` as `substring` is a part of `string1`\nassert_eq!(str_to_range(string1, substring), Some(4..7));\n\n// Returns `None` as `substring` is not a part of `string2`\nassert_eq!(str_to_range(string2, substring), None);\n```\n\n[CHANGELOG.md]: CHANGELOG.md\n\n[LineSpanExt]: https://docs.rs/line-span/*/line_span/trait.LineSpanExt.html\n[`LineSpan`]: https://docs.rs/line-span/*/line_span/struct.LineSpan.html\n[`LineSpanIter`]: https://docs.rs/line-span/*/line_span/struct.LineSpanIter.html\n[`LineSpans`]: https://docs.rs/line-span/*/line_span/trait.LineSpans.html\n[`line_spans()`]: https://docs.rs/line-span/*/line_span/trait.LineSpans.html#tymethod.line_spans\n[`Deref`]: https://doc.rust-lang.org/stable/std/ops/trait.Deref.html\n\n[`str_to_range()`]: https://docs.rs/line-span/*/line_span/fn.str_to_range.html\n[`str_to_range_unchecked()`]: https://docs.rs/line-span/*/line_span/fn.str_to_range_unchecked.html\n\n[`str`]: https://doc.rust-lang.org/stable/std/primitive.str.html\n[`\u0026str`]: https://doc.rust-lang.org/stable/std/primitive.str.html\n[`lines()`]: https://doc.rust-lang.org/stable/std/primitive.str.html#method.lines\n[`str::lines()`]: https://doc.rust-lang.org/stable/std/primitive.str.html#method.lines\n\n[`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvallentin%2Fline-span","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvallentin%2Fline-span","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvallentin%2Fline-span/lists"}