-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⦕ Complex brackets #23
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { areBracketsBalanced } from './utils'; | ||
|
||
describe('areBracketsBalanced', () => { | ||
it.each([ | ||
// Each test case is an object (or array) with the inputs and expected result | ||
{ input: '', expected: true }, | ||
{ input: '()', expected: true }, | ||
{ input: '()[]{}', expected: true }, | ||
{ input: '([{}])', expected: true }, | ||
{ input: '(([]){})', expected: true }, | ||
{ input: '{([([])])}', expected: true }, | ||
{ input: '(]', expected: false }, | ||
{ input: '([)]', expected: false }, | ||
{ input: '(()', expected: false }, | ||
{ input: '(()]', expected: false }, | ||
])('should return $expected for "$input"', ({ input, expected }) => { | ||
expect(areBracketsBalanced(input)).toBe(expected); | ||
}); | ||
|
||
it('should ignore non-bracket characters', () => { | ||
expect(areBracketsBalanced('abc(def)ghi')).toBe(true); | ||
expect(areBracketsBalanced('(abc]def')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export const BRACKETS: Record<string, string> = { | ||
'[': 'bracket.l', | ||
']': 'bracket.r', | ||
'{': 'brace.l', | ||
'}': 'brace.r', | ||
'(': 'paren.l', | ||
')': 'paren.r', | ||
'|': 'bar.v', | ||
lfloor: 'floor.l', | ||
'⌊': 'floor.l', | ||
rfloor: 'floor.r', | ||
'⌋': 'floor.r', | ||
rceil: 'ceil.r', | ||
'⌉': 'ceil.r', | ||
lceil: 'ceil.l', | ||
'⌈': 'ceil.l', | ||
}; | ||
|
||
export function areBracketsBalanced(input: string): boolean { | ||
// This stack will hold the opening brackets as they appear | ||
const stack: string[] = []; | ||
|
||
// A map from closing brackets to their corresponding opening bracket | ||
const bracketMap: Record<string, string> = { | ||
')': '(', | ||
']': '[', | ||
'}': '{', | ||
}; | ||
|
||
// Check each character in the string | ||
for (const char of input) { | ||
// If it’s an opening bracket, push it to the stack | ||
if (char === '(' || char === '[' || char === '{') { | ||
stack.push(char); | ||
} | ||
// If it’s a closing bracket, verify the top of the stack | ||
else if (char === ')' || char === ']' || char === '}') { | ||
// If stack is empty or the top of the stack doesn't match the correct opening bracket, it’s unbalanced | ||
if (!stack.length || bracketMap[char] !== stack.pop()) { | ||
return false; | ||
} | ||
} | ||
// Ignore other characters | ||
} | ||
|
||
// If the stack is empty, every opening bracket had a matching closing bracket | ||
return stack.length === 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably fine for now, since the unbalanced brackets are rare, so the most common case of balanced brackets will be fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my thought.
My original solution was just to do this all the time, however, that gives some pretty verbose exports for very simple things like
e_(f(x))
toe_(f paren.l x paren.r)
, which I didn't want to do. :)I think in the future we should have an intermediate tree that is meant for writing, and then do the unbalanced test on that tree, then write to string.
Put this PR together to unblock near term as that is going to be a larger change.