Skip to content

fixed error in entab that left more than minimum required spaces and tabs#100

Open
TimmyTheTaterTot wants to merge 2 commits intoohkimur:mainfrom
TimmyTheTaterTot:Exercise_1-21_Fix
Open

fixed error in entab that left more than minimum required spaces and tabs#100
TimmyTheTaterTot wants to merge 2 commits intoohkimur:mainfrom
TimmyTheTaterTot:Exercise_1-21_Fix

Conversation

@TimmyTheTaterTot
Copy link

@TimmyTheTaterTot TimmyTheTaterTot commented Dec 25, 2025

I noticed that the included solution was leaving extra unneeded spaces where a tab would have just sufficed. For example, with the input "Hello, \t\t \tWorld!" (just replace \t with the actual tab character to test that for example). Just added a base case to catch \t and insert them rather than inserting nr_spaces THEN the tab character.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of tab characters to preserve existing tabs in input while maintaining accurate line position tracking throughout processing.
    • Refined the space-to-tab conversion algorithm to better distinguish between tab and space characters during conversion.
    • Enhanced position management to correctly advance to the next tab stop when encountering tab characters.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 25, 2025

Walkthrough

A single C file was updated to explicitly preserve input tab characters: tabs now reset the accumulated-space counter, are emitted directly, and advance the column position to the next tab stop. Space-to-tab conversion remains for runs of spaces at tab boundaries.

Changes

Cohort / File(s) Summary
Tab character handling
chapter_1/exercise_1_21/entab.c
Reworked main loop control flow to distinguish '\t' from spaces: added an explicit branch that resets nr_of_spaces, outputs the tab, and advances line_pos to the next tab stop; adjusted line_pos increments for spaces and other characters and preserved existing space-to-tab conversion at tab boundaries.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main fix: handling tabs explicitly to avoid leaving unnecessary spaces, which aligns with the described bug and code changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5e7af42 and 0981a91.

📒 Files selected for processing (1)
  • chapter_1/exercise_1_21/entab.c
🔇 Additional comments (3)
chapter_1/exercise_1_21/entab.c (3)

11-18: LGTM: Space-to-tab conversion logic is correct.

The space handling correctly accumulates spaces and converts them to tabs when reaching tab boundaries with multiple accumulated spaces. The condition nr_of_spaces > 1 appropriately ensures only multi-space sequences are converted to tabs.


19-22: Excellent: Previous critical issue with tab position tracking is now fixed.

The tab handling correctly:

  • Resets nr_of_spaces (accumulated spaces before a tab are redundant since the tab advances to the next tab stop)
  • Outputs the tab character directly
  • Advances line_pos to the next tab stop using the formula: TAB_LENGTH - (line_pos % TAB_LENGTH)

The position-tracking formula is mathematically correct and resolves the critical issue flagged in the previous review. The implementation now properly maintains column position for subsequent tab-stop detection.


23-35: LGTM: Character handling and position tracking are correct.

The else block properly:

  • Flushes accumulated spaces before outputting the character
  • Updates line_pos to reflect the position after this character
  • Resets line_pos to 0 on newline for the next line
  • Outputs the character

The position tracking correctly maintains column state throughout, even though position updates are decoupled from the actual output of accumulated spaces.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
chapter_1/exercise_1_21/entab.c (1)

16-18: Consider the space-to-tab conversion algorithm limitations.

The current algorithm only converts spaces to tabs when reaching exact tab stop boundaries (line_pos % TAB_LENGTH == 0). This means sequences like 7 spaces followed by a non-space character will output 7 spaces, rather than converting to a tab + spaces.

This is a valid algorithm design for a simple entab implementation (convert only at tab stops), but be aware that it won't produce minimal output for all cases.

Example: " x" (7 spaces + 'x') at the start of a line will output as-is, but could be "\tx" (tab + 'x' would position at column 8, then backtrack with spaces if needed, though that's more complex).

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bdcee72 and 5e7af42.

📒 Files selected for processing (1)
  • chapter_1/exercise_1_21/entab.c

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.

1 participant