Skip to main content

Simple functions to move by words in a string

Project description

cursword

Simple functions to move by words in a string.

  • Useful for word-based cursors.
  • Use Unicode character classification to detect word boundaries.

Installation

pip install cursword

Quick Start

from cursword import get_next_word_end_position, get_previous_word_start_position

text = "Hello world, how are you?"

# Find the end of the first word starting from position 0
next_pos = get_next_word_end_position(text, 0)
print(f"First word ends at position {next_pos}: '{text[:next_pos]}'")
# Output: First word ends at position 5: 'Hello'

# Find the start of the last word from the end
prev_pos = get_previous_word_start_position(text, len(text))
print(f"Last word starts at position {prev_pos}: '{text[prev_pos:]}'")
# Output: Last word starts at position 24: '?'

API Reference

get_next_word_end_position(text: str, start: int) -> int

Returns the position of the end of the current or next word in the given text.

Parameters:

  • text (str): The text to search in
  • start (int): The position to start searching from

Returns:

  • int: The position after the end of the current/next word, or len(text) if no word is found

Example:

from cursword import get_next_word_end_position

text = "abc def ghi"
pos = get_next_word_end_position(text, 0)  # Returns 3 (end of "abc")
pos = get_next_word_end_position(text, pos)  # Returns 7 (end of "def")

get_previous_word_start_position(text: str, start: int) -> int

Returns the position of the start of the previous word in the given text.

Parameters:

  • text (str): The text to search in
  • start (int): The position to start searching from

Returns:

  • int: The position of the start of the previous word, or 0 if no word is found

Example:

from cursword import get_previous_word_start_position

text = "abc def ghi"
pos = get_previous_word_start_position(text, len(text))  # Returns 8 (start of "ghi")
pos = get_previous_word_start_position(text, pos)  # Returns 4 (start of "def")

How It Works

The library categorizes characters into different types:

  • Word characters: Letters, numbers, and underscores
  • Punctuation: Various punctuation marks and mathematical symbols
  • Currency: Currency symbols
  • Space: Whitespace characters
  • Other: All other characters (including CJK ideographs)

Word boundaries are detected when transitioning between different character categories, allowing for intelligent navigation through mixed content.

Known limitations

  • CJK Text: Chinese, Japanese, and Korean characters are currently treated as single blocks rather than individual word units.

Development

# Best suited for uv
uv sync

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=cursword --cov-report=html --cov-report=term-missing

# Lint code
uv run ruff check

# Format code
uv run ruff format

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cursword-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cursword-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file cursword-0.1.0.tar.gz.

File metadata

  • Download URL: cursword-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for cursword-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a59eda1f520159ceb3bda8253dae6134df87de8900e91931f6ae33ad8f323fc6
MD5 b49c3a48b079791c6d22bb381fb123af
BLAKE2b-256 2bdd392b1deb1af9a252f6130e137eb09a01e52eac42a435055b1854a6fa6efa

See more details on using hashes here.

File details

Details for the file cursword-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cursword-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for cursword-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be09d1b5f2c4512b13580981db2cb13b7cf62983692edd8243976ad545a47023
MD5 b8a079998b2136b3d7fcd8948265cfa8
BLAKE2b-256 c7143c94c5341d4dd45b825d5788b08e3e44a56a3be187148f2735c7404a98cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page