Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

schlib improvement to handle double-quote text in symbol user fields #324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eeintech
Copy link

While working on my little tool to parse symbol library files, I've noticed that there was a little bug to handle user fields (F3 and up) when a double-quote was inserted in the text field, the library parser was handling it as an escape quote. It appears to come from the shlex command parser which has no insight on how to treat this character.

I've tried to solve it using shlex but eventually gave up, I even posted on StackOverflow:
https://stackoverflow.com/questions/60877782/python-shlex-posix-usage-dilemma

Instead, I've found regex pattern matching for user fields containing double-quotes works well, based on this answer:
https://stackoverflow.com/a/16710842/12794913

I've tested this change quite extensively as I've been doing a lot of library clean-up for my company.
I'm open to other implementation, just wanted to give a heads-up on this little annoying bug.

Here is a little code snippet if you want to understand the issue I'm referring to:

import shlex

string = '\"This is a \\"difficult\\" problem\" please help'
print(f'string = {string}')

s1 = shlex.shlex(string, posix=False)
slist1 = list(s1)
# Not the expected result
print(f'slist1 = {slist1}')

s2 = shlex.shlex(string, posix=True)
slist2 = list(s2)
# Not the expected result
print(f'slist2 = {slist2}')

import re

slist3 = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', string)
# It works, yay!
print(f'slist3 = {slist3}')

Comment on lines +156 to +158
if '\\"' in line:
import re
line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', line)
Copy link
Author

Choose a reason for hiding this comment

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

If double-quote detected in line, split using regex instead of posix

…t contains double-quotes (ex: in reference to 'inch' imperial unit)
@eeintech eeintech force-pushed the schlib_double_quote_in_field branch from 37813e1 to 36eebf3 Compare August 3, 2020 01:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant