{"id":27376290,"url":"https://github.com/julesguesnon/nom-span","last_synced_at":"2025-11-11T11:08:26.397Z","repository":{"id":207580978,"uuid":"719598930","full_name":"JulesGuesnon/nom-span","owner":"JulesGuesnon","description":"📦 Wrap your nom input into a span to get line, column and byte offset","archived":false,"fork":false,"pushed_at":"2024-10-09T13:49:06.000Z","size":23,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T03:06:01.125Z","etag":null,"topics":["nom","nom-span","parser","rust","span"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/nom-span","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/JulesGuesnon.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-11-16T14:05:53.000Z","updated_at":"2025-03-24T07:34:00.000Z","dependencies_parsed_at":"2023-11-16T15:43:45.930Z","dependency_job_id":null,"html_url":"https://github.com/JulesGuesnon/nom-span","commit_stats":null,"previous_names":["julesguesnon/nom-span"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulesGuesnon%2Fnom-span","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulesGuesnon%2Fnom-span/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulesGuesnon%2Fnom-span/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulesGuesnon%2Fnom-span/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JulesGuesnon","download_url":"https://codeload.github.com/JulesGuesnon/nom-span/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248715206,"owners_count":21150060,"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":["nom","nom-span","parser","rust","span"],"created_at":"2025-04-13T12:36:15.045Z","updated_at":"2025-11-11T11:08:26.365Z","avatar_url":"https://github.com/JulesGuesnon.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nom Span \u0026emsp; [![Build Status]][actions] [![Latest Version]][crates.io]\n\n[Build Status]: https://img.shields.io/github/actions/workflow/status/julesguesnon/nom-span/rust.yml?branch=main\n[actions]: https://github.com/julesguesnon/nom-span/actions?query=branch%3Amain\n[crates.io]: https://crates.io/crates/nom-span\n[Latest Version]: https://img.shields.io/crates/v/nom-span.svg\n\nThis library expose `Spanned`, a struct that will wraps your input and allow you to keep track of the line number, the column number and the byte offset\n\n## How to use it?\n\nHere is a basic example of how to create the input and how to retrieve all the informations you need.\n\n```rust\nuse nom_span::Spanned;\n\ntype Span\u003c'a\u003e = Spanned\u003c\u0026'a str\u003e;\n\nfn main() {\n    let span = Span::new(\n      r#\"{\"hello\": \"world 🙌\"}\"#,\n      // Supporting UTF-8\n      true\n    );\n\n    assert_eq!(span.line(), 1);\n    assert_eq!(span.col(), 1);\n    assert_eq!(span.byte_offset(), 0);\n}\n```\n\nYou can notice that supporting UTF-8 is optional. The reason is that UTF-8 strings need to be handled in a different way than pure ASCII strings, and thus, there can be a performance gap with UTF-8 support (see the benchmark below)\n\n### UTF-8 and ASCII comparison\n\nA UTF-8 char can be made of 1 to 4 bytes, so counting it the ASCII way would result in counting each byte of the UTF-8 char, and will result in unexpected column number:\n\n```rust\nuse nom_span::Spanned;\n\ntype Span\u003c'a\u003e = Spanned\u003c\u0026'a str\u003e;\n\nfn utf8_vs_ascii() {\n    let utf8 = Span::new(\"🙌\", true);\n    let ascii = Span::new(\"🙌\", false);\n\n    let utf8_after: IResult\u003cSpan\u003c'_\u003e, Vec\u003cchar\u003e\u003e = many1(anychar)(utf8);\n    let ascii_after: IResult\u003cSpan\u003c'_\u003e, Vec\u003cchar\u003e\u003e = many1(anychar)(ascii);\n\n    let (utf8_after, _) = utf8_after.unwrap();\n    let (ascii_after, _) = ascii_after.unwrap();\n\n    assert_eq!(utf8_after.col(), 2);\n    assert_eq!(ascii_after.col(), 5);\n}\n\n```\n\n## What about [nom_locate](https://github.com/fflorent/nom_locate)?\n\nI was initially using nom_locate, but I faced some huge performance issue while building a [json parser](https://github.com/julesguesnon/spanned-json-parser), so I decided to implement my own input. I basically cloned nom_locate and modified the counting function that was causing the performance issue. So thanks a lot for this awesome crate and please go add a star to it!\n\n### What's the difference with [nom_locate](https://github.com/fflorent/nom_locate)?\n\nnom_locate is recounting all the chars of your entire input (even if you already consumed it) when you're calling `get_column`. If you're calling `get_column` every char, runtime would be: `O(N^2)`\nWith this crate, it's counting lines and columns everytime you're consuming your input. If you're calling `col` every char, runtime would be: `O(2N)`\n\nSo if you're planning to get the column only a few times, for example, only when an error occur, it may be better to use nom_locate, but if you need it quite often, this crate should be better.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulesguesnon%2Fnom-span","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjulesguesnon%2Fnom-span","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulesguesnon%2Fnom-span/lists"}