Skip to content

[FEATURE] Port character encoding conversions to Rust - #2487

Open
Aditya-Saroha wants to merge 1 commit into
CCExtractor:masterfrom
Aditya-Saroha:feat/port-char-encoding-to-rust
Open

Aditya-Saroha wants to merge 1 commit into
CCExtractor:masterfrom
Aditya-Saroha:feat/port-char-encoding-to-rust

Conversation

@Aditya-Saroha

Copy link
Copy Markdown
Contributor

[FEATURE] Port character encoding conversions to Rust

In raising this pull request, I confirm the following (please check boxes):

Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog. If it's just a bug fix, I have NOT added it to the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Repro instructions:

N/A - This PR ports existing character encoding logic from C to Rust following docs/Rust_migration_guide.md. Unit tests verify equivalence across all 256 byte values and special character encodings.


Summary

This PR ports the character encoding conversion and case-mapping functions from src/lib_ccx/ccx_common_char_encoding.c into pure Rust (lib_ccxr) and wires them through C-FFI exports (libccxr_exports), following the architectural conventions in docs/Rust_migration_guide.md.

Functions Ported

C Function Rust Export (libccxr_exports) Core Pure Rust Implementation (lib_ccxr)
get_char_in_latin_1(buf, c) ccxr_get_char_in_latin_1 lib_ccxr::util::encoding::line21_to_latin1(c)
get_char_in_unicode(buf, c) ccxr_get_char_in_unicode lib_ccxr::util::encoding::line21_to_ucs2(c) (little-endian 2-byte write)
get_char_in_utf_8(buf, c) ccxr_get_char_in_utf_8 lib_ccxr::util::encoding::line21_to_utf8(c) (returns length 1–3)
cctolower(c) ccxr_cctolower lib_ccxr::util::encoding::cctolower(c)
cctoupper(c) ccxr_cctoupper lib_ccxr::util::encoding::cctoupper(c)

Architectural Changes

  1. lib_ccxr::util::encoding:

    • Made byte-level cctolower(c: u8) -> u8 and cctoupper(c: u8) -> u8 public functions.
    • Refactored Line21String, Latin1String, and Ucs2String casing methods to use cctolower/cctoupper directly. This eliminates potential panics from char::from_u32(c).expect(...) on unmapped/surrogate byte values.
    • Added extensive unit tests covering ASCII casing, extended character casing, Latin-1/UCS-2/UTF-8 conversions, and a 0..=255 byte iteration test ensuring zero panics.
  2. libccxr_exports::encoding:

    • Added C-ABI extern "C" wrappers with null-pointer guards.
    • Handles buffer pointer writes and returns matching the existing C ABI.
    • Includes unit tests verifying pointer safety, return values, and byte outputs.
  3. C Integration:

    • Wrapped implementations in src/lib_ccx/ccx_common_char_encoding.c with #ifndef DISABLE_RUST delegating to ccxr_*.
    • The original C implementation is retained under #else so non-Rust builds (DISABLE_RUST) continue to compile and function without modification.

Verification

  • Unit Tests (lib_ccxr):
    cargo test --manifest-path src/rust/lib_ccxr/Cargo.toml --lib
    Result: 63 passed, 0 failed.
  • Code Style & Linting:
    cargo fmt --manifest-path src/rust/lib_ccxr/Cargo.toml --check
    cargo clippy --manifest-path src/rust/lib_ccxr/Cargo.toml
    Result: Clean, 0 warnings.
  • C Compilation:
    Verified compilation with GCC both with Rust enabled (gcc -c src/lib_ccx/ccx_common_char_encoding.c -Isrc/lib_ccx) and with -DDISABLE_RUST.

- Port get_char_in_latin_1, get_char_in_unicode, and get_char_in_utf_8 to lib_ccxr and libccxr_exports

- Export cctolower and cctoupper through libccxr_exports

- Hook C functions to Rust under #ifndef DISABLE_RUST

- Preserve Latin-1 0x87 music note substitution as pilcrow (0xB6) for exact CI sample platform parity

- Add unit tests for encodings, case mappings, and FFI pointer safety
@Aditya-Saroha
Aditya-Saroha force-pushed the feat/port-char-encoding-to-rust branch from c75398b to 1b2d7f2 Compare November 24, 2026 08:18
@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on linux. 157/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 19/28
Hardsubx 1/1
Hauppage 2/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 26/34

81 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10124, commit 034fdbc:

  • 14 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Pass there, fail here:

  • ccextractor --autoprogram --out=ttxt --latin1 9a496d3828...
  • ccextractor --autoprogram --out=ttxt --latin1 c032183ef0...
  • ccextractor --autoprogram --out=ttxt --latin1 132d7df7e9...
  • ccextractor --autoprogram --out=ttxt --latin1 99e5eaafdc...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 5d3a29f9f8...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 70000200c0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --output-field 2 5d3a29f9f8...
  • ccextractor --autoprogram --out=ttxt --hauppauge --ucla --latin1 553d78e755...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 725a49f871...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --xds --ucla c813e713a0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds b992e0cccb...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 53339f3455...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 88cd42b89a...

Compared with the commit this branch was cut from: the same run as the tip of master (test 10124), so the comparison above already covers it.


This branch changes the behaviour of 14 test(s) relative to the tip of master. Those are the ones worth looking at; anything else in the list fails the same way on both sides.

@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on windows. 157/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 19/28
Hardsubx 1/1
Hauppage 2/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 26/34

81 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10125, commit 034fdbc:

  • 14 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Pass there, fail here:

  • ccextractor --autoprogram --out=ttxt --latin1 9a496d3828...
  • ccextractor --autoprogram --out=ttxt --latin1 c032183ef0...
  • ccextractor --autoprogram --out=ttxt --latin1 132d7df7e9...
  • ccextractor --autoprogram --out=ttxt --latin1 99e5eaafdc...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 5d3a29f9f8...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 70000200c0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --output-field 2 5d3a29f9f8...
  • ccextractor --autoprogram --out=ttxt --hauppauge --ucla --latin1 553d78e755...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 725a49f871...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --xds --ucla c813e713a0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds b992e0cccb...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 53339f3455...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 88cd42b89a...

Compared with the commit this branch was cut from: the same run as the tip of master (test 10125), so the comparison above already covers it.


This branch changes the behaviour of 14 test(s) relative to the tip of master. Those are the ones worth looking at; anything else in the list fails the same way on both sides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants