-
Notifications
You must be signed in to change notification settings - Fork 14
Handle double quotes, backslashes, CRs, NLs and tabs #3
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
Open
gtd
wants to merge
10
commits into
rylev:master
Choose a base branch
from
mubi:basic_escapes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8bee90d
added gitignore file
jmoody baecf82
added additional specs for parsing ASCII files. commented out some c…
jmoody 709ad5c
refactored create_temp_file to take (required) encoding argument
jmoody 60329bd
cannot reproduce bug using Tempfile + encoding - will try with actual…
jmoody 7a6349e
added some tests around encoding
jmoody 467bf0c
solved the problem with BOM at the beginning of strings files
jmoody 21e8504
That's what allow rake spec to work.
gtd 5afc5de
Gemfile.lock removed (previously only ignored)
gtd 86f3750
Move spec sample files into subdirectory
gtd 653366a
Handle basic escape sequences in values
gtd 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 hidden or 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,29 @@ | ||
| *.gem | ||
| *.rbc | ||
| .bundle | ||
| .config | ||
| .yardoc | ||
| Gemfile.lock | ||
| InstalledFiles | ||
| _yardoc | ||
| vendor | ||
| coverage | ||
| doc/ | ||
| lib/bundler/man | ||
| pkg | ||
| rdoc | ||
| spec/reports | ||
| test/tmp | ||
| test/version_tmp | ||
| tmp | ||
|
|
||
| # JetBrains | ||
| .idea | ||
|
|
||
| # mac | ||
| .DS_Store | ||
| __MACOSX | ||
|
|
||
| # emacs turds | ||
| (.*/)?\#[^/]*\#$ | ||
|
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -61,12 +61,47 @@ def key | |
|
|
||
| def value | ||
| if key_value_pair? | ||
| cleaned_content.partition(/"\s*=\s*"/)[2].gsub!(/(^"|"$)/, "") | ||
| unescape_value cleaned_content.partition(/"\s*=\s*"/)[2].gsub!(/(^"|"$)/, "") | ||
| end | ||
| end | ||
|
|
||
| def is_comment? | ||
| whole_comment? || open_comment? || close_comment? || in_comment | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html | ||
| def unescape_value(string) | ||
| state = :normal | ||
| out = '' | ||
| string.each_char do |c| | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think that "char" is better than "c" |
||
| case state | ||
| when :normal | ||
| if c == '\\' | ||
| state = :escape | ||
| else | ||
| out += c | ||
| end | ||
| when :escape | ||
| state = :normal | ||
| case c | ||
| when '\\' | ||
| out += '\\' | ||
| when '"' | ||
| out += '"' | ||
| when 'r' | ||
| out += "\r" | ||
| when 'n' | ||
| out += "\n" | ||
| when 't' | ||
| out += "\t" | ||
| else | ||
| out += '\\' + c # Do nothing, however in the future handling unicode escapes could be good | ||
| end | ||
| end | ||
| end | ||
| out | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,32 @@ | ||
| require 'spec_helper' | ||
| require 'apfel' | ||
|
|
||
| describe Apfel do | ||
| describe '::parse_file' do | ||
| context 'when given DotStrings file with escapes'do | ||
| let(:parsed_file_hash) do | ||
| Apfel.parse('./spec/files/escapes.strings').to_hash(:with_comments => false) | ||
| end | ||
|
|
||
| it 'should parse nl' do | ||
| parsed_file_hash['multiline'].should eq "line 1\nline 2" | ||
| end | ||
|
|
||
| it 'should parse cr' do | ||
| parsed_file_hash['mac'].should eq "before cr\rafter cr" | ||
| end | ||
|
|
||
| it 'should parse tabs' do | ||
| parsed_file_hash['tabs'].should eq "two spaces \t equals tab" | ||
| end | ||
|
|
||
| it 'should parse double quotes' do | ||
| parsed_file_hash['dq'].should eq "\"someone said this\"" | ||
| end | ||
|
|
||
| it 'should parse backslashes' do | ||
| parsed_file_hash['backslash'].should eq "\\not a forward slash" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,50 @@ | ||
| require 'spec_helper' | ||
| require 'apfel' | ||
| require 'apfel/parsed_dot_strings' | ||
|
|
||
| describe Apfel do | ||
| describe '::parse_file' do | ||
| context 'when given a ASCII DotStrings file'do | ||
|
|
||
| it 'the file should be ascii' do | ||
| res = `file -I ./spec/files/ascii.strings` | ||
| encoding = res.split(/=/).last.gsub!("\n",'') | ||
| encoding.should == 'us-ascii' | ||
| end | ||
|
|
||
| let(:parsed_file) do | ||
| Apfel.parse('./spec/files/ascii.strings') | ||
| end | ||
|
|
||
| it 'returns a ParsedDotStrings object' do | ||
| parsed_file.should be_a(Apfel::ParsedDotStrings) | ||
| end | ||
|
|
||
| #it 'should have the correct keys' do | ||
| # parsed_file.keys.should include 'key_number_one' | ||
| # parsed_file.keys.should include 'key_number_two' | ||
| # parsed_file.keys.should include 'key_number_three' | ||
| #end | ||
| # | ||
| #it 'should have the correct values' do | ||
| # parsed_file.values.should include 'value number one' | ||
| # parsed_file.values.should include 'value number two' | ||
| # parsed_file.values.should include 'value number three' | ||
| #end | ||
| # | ||
| describe 'should have the correct comments' do | ||
| it 'should have the correct comment for avoided social event' do | ||
| parsed_file.comments['avoided social event'].should == 'No comment provided by engineer.' | ||
| end | ||
|
|
||
| it 'should have the correct comment for binged' do | ||
| parsed_file.comments['binged'].should == 'No comment provided by engineer.' | ||
| end | ||
|
|
||
| it 'should have the correct comment for called a friend' do | ||
| parsed_file.comments['called a friend'].should == 'No comment provided by engineer.' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,53 @@ | ||
| require 'spec_helper' | ||
| require 'apfel' | ||
| require 'apfel/parsed_dot_strings' | ||
|
|
||
| describe Apfel do | ||
| describe '::parse_file' do | ||
| context 'when given a UTF8 DotStrings file'do | ||
|
|
||
| it 'the file should be utf-8' do | ||
| File.open('./spec/files/utf8.strings', 'r') do |f| | ||
| f.external_encoding.name.should == 'UTF-8' | ||
| content = f.read | ||
| content.encoding.name.should == 'UTF-8' | ||
| end | ||
| end | ||
|
|
||
| let(:parsed_file) do | ||
| Apfel.parse('./spec/files/utf8.strings') | ||
| end | ||
|
|
||
| it 'returns a ParsedDotStrings object' do | ||
| parsed_file.should be_a(Apfel::ParsedDotStrings) | ||
| end | ||
|
|
||
| #it 'should have the correct keys' do | ||
| # parsed_file.keys.should include 'key_number_one' | ||
| # parsed_file.keys.should include 'key_number_two' | ||
| # parsed_file.keys.should include 'key_number_three' | ||
| #end | ||
| # | ||
| #it 'should have the correct values' do | ||
| # parsed_file.values.should include 'value number one' | ||
| # parsed_file.values.should include 'value number two' | ||
| # parsed_file.values.should include 'value number three' | ||
| #end | ||
| # | ||
| describe 'should have the correct comments' do | ||
| it 'should have the correct comment for anger' do | ||
| parsed_file.comments['anger'].should == 'No comment provided by engineer.' | ||
| end | ||
|
|
||
| it 'should have the correct comment for anxiety' do | ||
| parsed_file.comments['anxiety'].should == 'No comment provided by engineer.' | ||
| end | ||
|
|
||
| it 'should have the correct comment for boredom' do | ||
| parsed_file.comments['boredom'].should == 'No comment provided by engineer.' | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,9 @@ | ||
| /* No comment provided by engineer. */ | ||
| "avoided social event" = "avoided social event"; | ||
|
|
||
| /* No comment provided by engineer. */ | ||
| "binged" = "binged"; | ||
|
|
||
| /* No comment provided by engineer. */ | ||
| "called a friend" = "called a friend"; | ||
|
|
This file contains hidden or 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,5 @@ | ||
| "multiline" = "line 1\nline 2"; | ||
| "mac" = "before cr\rafter cr"; | ||
| "tabs" = "two spaces \t equals tab"; | ||
| "dq" = "\"someone said this\""; | ||
| "backslash" = "\\not a forward slash"; |
This file contains hidden or 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,8 @@ | ||
| /* No comment provided by engineer. */ | ||
| "anger" = "anger"; | ||
|
|
||
| /* No comment provided by engineer. */ | ||
| "anxiety" = "anxiety"; | ||
|
|
||
| /* No comment provided by engineer. */ | ||
| "boredom" = "boredom"; |
This file contains hidden or 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.
I don't like the name "out". How about "output"?