minimindslab-line-column-map 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MiniMindsLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: minimindslab-line-column-map
3
+ Version: 0.1.0
4
+ Summary: Map absolute character offsets to 1-based line and column positions in text, with optional reverse lookup from line/column to offset.
5
+ Author: MiniMindsLab
6
+ License: MIT
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # MiniMindsLab Line Column Map
13
+
14
+ Tiny deterministic helpers for mapping:
15
+
16
+ - absolute character offsets -> **1-based** `(line, column)`
17
+ - **1-based** `(line, column)` -> absolute character offsets
18
+
19
+ Works with mixed newline styles (`\n`, `\r\n`, `\r`) and uses only Python standard library.
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ pip install minimindslab-line-column-map
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```python
30
+ from minimindslab_line_column_map import offset_to_position, position_to_offset
31
+
32
+ text = "a\nβ\r\nc\r"
33
+
34
+ line, col = offset_to_position(text, 3) # -> (2, 2)
35
+ offset = position_to_offset(text, line=2, column=2) # -> 3
36
+ ```
37
+
38
+ ## Behavior
39
+
40
+ - Lines and columns are 1-based.
41
+ - Offset must be in `[0, len(text)]`.
42
+ - `(line, column)` must refer to a valid insertion point within the selected line.
43
+ - Newline sequences are treated as line breaks; `\r\n` is a single break.
44
+
45
+ ## API
46
+
47
+ - `offset_to_position(text: str, offset: int) -> tuple[int, int]`
48
+ - `position_to_offset(text: str, line: int, column: int) -> int`
49
+ - `LineColumnMap(text: str)` with methods:
50
+ - `.offset_to_position(offset)`
51
+ - `.position_to_offset(line, column)`
@@ -0,0 +1,40 @@
1
+ # MiniMindsLab Line Column Map
2
+
3
+ Tiny deterministic helpers for mapping:
4
+
5
+ - absolute character offsets -> **1-based** `(line, column)`
6
+ - **1-based** `(line, column)` -> absolute character offsets
7
+
8
+ Works with mixed newline styles (`\n`, `\r\n`, `\r`) and uses only Python standard library.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ pip install minimindslab-line-column-map
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```python
19
+ from minimindslab_line_column_map import offset_to_position, position_to_offset
20
+
21
+ text = "a\nβ\r\nc\r"
22
+
23
+ line, col = offset_to_position(text, 3) # -> (2, 2)
24
+ offset = position_to_offset(text, line=2, column=2) # -> 3
25
+ ```
26
+
27
+ ## Behavior
28
+
29
+ - Lines and columns are 1-based.
30
+ - Offset must be in `[0, len(text)]`.
31
+ - `(line, column)` must refer to a valid insertion point within the selected line.
32
+ - Newline sequences are treated as line breaks; `\r\n` is a single break.
33
+
34
+ ## API
35
+
36
+ - `offset_to_position(text: str, offset: int) -> tuple[int, int]`
37
+ - `position_to_offset(text: str, line: int, column: int) -> int`
38
+ - `LineColumnMap(text: str)` with methods:
39
+ - `.offset_to_position(offset)`
40
+ - `.position_to_offset(line, column)`
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0.3"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "minimindslab-line-column-map"
7
+ version = "0.1.0"
8
+ description = "Map absolute character offsets to 1-based line and column positions in text, with optional reverse lookup from line/column to offset."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = { text = "MIT" }
12
+ authors = [
13
+ { name = "MiniMindsLab" }
14
+ ]
15
+ dependencies = []
16
+
17
+ [tool.setuptools]
18
+ package-dir = {"" = "src"}
19
+
20
+ [tool.setuptools.packages.find]
21
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,85 @@
1
+ """Map absolute offsets to 1-based line/column and back."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from bisect import bisect_right
6
+
7
+ __all__ = ["LineColumnMap", "offset_to_position", "position_to_offset"]
8
+
9
+
10
+ class LineColumnMap:
11
+ """Index text for deterministic offset/position conversion.
12
+
13
+ Newline handling:
14
+ - "\n" line break
15
+ - "\r\n" line break (single break)
16
+ - "\r" line break
17
+ """
18
+
19
+ def __init__(self, text: str) -> None:
20
+ if not isinstance(text, str):
21
+ raise TypeError("text must be a str")
22
+ self._text = text
23
+ self._line_starts = self._compute_line_starts(text)
24
+
25
+ @staticmethod
26
+ def _compute_line_starts(text: str) -> list[int]:
27
+ starts = [0]
28
+ i = 0
29
+ n = len(text)
30
+ while i < n:
31
+ ch = text[i]
32
+ if ch == "\n":
33
+ starts.append(i + 1)
34
+ i += 1
35
+ elif ch == "\r":
36
+ if i + 1 < n and text[i + 1] == "\n":
37
+ starts.append(i + 2)
38
+ i += 2
39
+ else:
40
+ starts.append(i + 1)
41
+ i += 1
42
+ else:
43
+ i += 1
44
+ return starts
45
+
46
+ def offset_to_position(self, offset: int) -> tuple[int, int]:
47
+ if not isinstance(offset, int):
48
+ raise TypeError("offset must be an int")
49
+ n = len(self._text)
50
+ if offset < 0 or offset > n:
51
+ raise ValueError("offset out of range")
52
+
53
+ idx = bisect_right(self._line_starts, offset) - 1
54
+ line = idx + 1
55
+ column = offset - self._line_starts[idx] + 1
56
+ return line, column
57
+
58
+ def position_to_offset(self, line: int, column: int) -> int:
59
+ if not isinstance(line, int) or not isinstance(column, int):
60
+ raise TypeError("line and column must be ints")
61
+ if line < 1 or column < 1:
62
+ raise ValueError("line and column must be >= 1")
63
+
64
+ if line > len(self._line_starts):
65
+ raise ValueError("line out of range")
66
+
67
+ line_start = self._line_starts[line - 1]
68
+ if line < len(self._line_starts):
69
+ line_end = self._line_starts[line]
70
+ else:
71
+ line_end = len(self._text)
72
+
73
+ max_column = (line_end - line_start) + 1
74
+ if column > max_column:
75
+ raise ValueError("column out of range")
76
+
77
+ return line_start + (column - 1)
78
+
79
+
80
+ def offset_to_position(text: str, offset: int) -> tuple[int, int]:
81
+ return LineColumnMap(text).offset_to_position(offset)
82
+
83
+
84
+ def position_to_offset(text: str, line: int, column: int) -> int:
85
+ return LineColumnMap(text).position_to_offset(line, column)
@@ -0,0 +1,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: minimindslab-line-column-map
3
+ Version: 0.1.0
4
+ Summary: Map absolute character offsets to 1-based line and column positions in text, with optional reverse lookup from line/column to offset.
5
+ Author: MiniMindsLab
6
+ License: MIT
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # MiniMindsLab Line Column Map
13
+
14
+ Tiny deterministic helpers for mapping:
15
+
16
+ - absolute character offsets -> **1-based** `(line, column)`
17
+ - **1-based** `(line, column)` -> absolute character offsets
18
+
19
+ Works with mixed newline styles (`\n`, `\r\n`, `\r`) and uses only Python standard library.
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ pip install minimindslab-line-column-map
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```python
30
+ from minimindslab_line_column_map import offset_to_position, position_to_offset
31
+
32
+ text = "a\nβ\r\nc\r"
33
+
34
+ line, col = offset_to_position(text, 3) # -> (2, 2)
35
+ offset = position_to_offset(text, line=2, column=2) # -> 3
36
+ ```
37
+
38
+ ## Behavior
39
+
40
+ - Lines and columns are 1-based.
41
+ - Offset must be in `[0, len(text)]`.
42
+ - `(line, column)` must refer to a valid insertion point within the selected line.
43
+ - Newline sequences are treated as line breaks; `\r\n` is a single break.
44
+
45
+ ## API
46
+
47
+ - `offset_to_position(text: str, offset: int) -> tuple[int, int]`
48
+ - `position_to_offset(text: str, line: int, column: int) -> int`
49
+ - `LineColumnMap(text: str)` with methods:
50
+ - `.offset_to_position(offset)`
51
+ - `.position_to_offset(line, column)`
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/minimindslab_line_column_map/__init__.py
5
+ src/minimindslab_line_column_map.egg-info/PKG-INFO
6
+ src/minimindslab_line_column_map.egg-info/SOURCES.txt
7
+ src/minimindslab_line_column_map.egg-info/dependency_links.txt
8
+ src/minimindslab_line_column_map.egg-info/top_level.txt
9
+ tests/test_line_column_map.py
@@ -0,0 +1,71 @@
1
+ import unittest
2
+
3
+ from minimindslab_line_column_map import LineColumnMap, offset_to_position, position_to_offset
4
+
5
+
6
+ class LineColumnMapTests(unittest.TestCase):
7
+ def test_mixed_newlines_deterministic_1_based(self):
8
+ text = "ab\ncd\r\nef\rgh"
9
+ m = LineColumnMap(text)
10
+
11
+ self.assertEqual(m.offset_to_position(0), (1, 1))
12
+ self.assertEqual(m.offset_to_position(2), (1, 3))
13
+ self.assertEqual(m.offset_to_position(3), (2, 1))
14
+ self.assertEqual(m.offset_to_position(7), (3, 1))
15
+ self.assertEqual(m.offset_to_position(len(text)), (4, 3))
16
+
17
+ def test_round_trip_valid_inputs(self):
18
+ text = "a\nβ\r\nc\r"
19
+ m = LineColumnMap(text)
20
+ for offset in range(0, len(text) + 1):
21
+ line, col = m.offset_to_position(offset)
22
+ self.assertEqual(m.position_to_offset(line, col), offset)
23
+
24
+ self.assertEqual(offset_to_position(text, 3), (2, 2))
25
+ self.assertEqual(position_to_offset(text, 2, 2), 3)
26
+
27
+ def test_crlf_newline_positions_allow_round_trip(self):
28
+ text = "x\r\ny"
29
+ m = LineColumnMap(text)
30
+ for offset in range(0, len(text) + 1):
31
+ line, col = m.offset_to_position(offset)
32
+ self.assertEqual(m.position_to_offset(line, col), offset)
33
+
34
+ def test_empty_singleline_trailing_newline_cases(self):
35
+ m_empty = LineColumnMap("")
36
+ self.assertEqual(m_empty.offset_to_position(0), (1, 1))
37
+ self.assertEqual(m_empty.position_to_offset(1, 1), 0)
38
+
39
+ m_single = LineColumnMap("hello")
40
+ self.assertEqual(m_single.offset_to_position(5), (1, 6))
41
+ self.assertEqual(m_single.position_to_offset(1, 6), 5)
42
+
43
+ m_trailing = LineColumnMap("x\n")
44
+ self.assertEqual(m_trailing.offset_to_position(2), (2, 1))
45
+ self.assertEqual(m_trailing.position_to_offset(2, 1), 2)
46
+
47
+ def test_invalid_offsets_and_positions_raise_value_error(self):
48
+ m = LineColumnMap("abc\n")
49
+ with self.assertRaises(ValueError):
50
+ m.offset_to_position(-1)
51
+ with self.assertRaises(ValueError):
52
+ m.offset_to_position(10)
53
+ with self.assertRaises(ValueError):
54
+ m.position_to_offset(0, 1)
55
+ with self.assertRaises(ValueError):
56
+ m.position_to_offset(1, 0)
57
+ with self.assertRaises(ValueError):
58
+ m.position_to_offset(10, 1)
59
+ with self.assertRaises(ValueError):
60
+ m.position_to_offset(1, 99)
61
+
62
+ def test_type_errors(self):
63
+ m = LineColumnMap("abc")
64
+ with self.assertRaises(TypeError):
65
+ m.offset_to_position(1.2)
66
+ with self.assertRaises(TypeError):
67
+ m.position_to_offset("1", 1)
68
+
69
+
70
+ if __name__ == "__main__":
71
+ unittest.main()