@@ -15,12 +15,12 @@ const repeatingBackSlashRegex = ctRegex!(`(\\)*$`);
1515
1616 Params:
1717 jsonString = the json string you want to minify
18- hasComments = a switch that indicates if the json string has comments. Pass `true` to support parsing comments. Default: `false`.
18+ hasComment = a boolean to support comments in json . Default: `false`.
1919
2020 Return:
2121 the minified json string
2222*/
23- string minifyString (string jsonString, bool hasComments = false ) @trusted
23+ string minifyString (string jsonString, bool hasComment = false ) @trusted
2424{
2525 auto in_string = false ;
2626 auto in_multiline_comment = false ;
@@ -29,7 +29,7 @@ string minifyString(string jsonString, bool hasComments = false) @trusted
2929 size_t from = 0 ;
3030 auto rightContext = " " ;
3131
32- const tokenizer = ! hasComments ? tokenizerNoComment : tokenizerWithComment;
32+ const tokenizer = ! hasComment ? tokenizerNoComment : tokenizerWithComment;
3333
3434 auto match = jsonString.matchAll(tokenizer);
3535
@@ -43,7 +43,8 @@ string minifyString(string jsonString, bool hasComments = false) @trusted
4343 const prevFrom = from;
4444 from = jsonString.length - rightContext.length; // lastIndex
4545
46- const noCommentOrNotInComment = ! hasComments || (! in_multiline_comment && ! in_singleline_comment);
46+ const notInComment = (! in_multiline_comment && ! in_singleline_comment);
47+ const noCommentOrNotInComment = ! hasComment || notInComment;
4748
4849 if (noCommentOrNotInComment)
4950 {
@@ -65,15 +66,11 @@ string minifyString(string jsonString, bool hasComments = false) @trusted
6566 -- from; // include " character in next catch
6667 rightContext = jsonString[from .. $];
6768 }
68- else if (matchFrontHit.matchFirst(spaceOrBreakRegex).empty())
69- {
70- new_str ~= matchFrontHit;
71- }
7269 }
7370 // comments
74- if (hasComments && ! in_string)
71+ if (hasComment && ! in_string)
7572 {
76- if (! in_multiline_comment && ! in_singleline_comment )
73+ if (notInComment )
7774 {
7875 if (matchFrontHit == " /*" )
7976 {
@@ -83,6 +80,10 @@ string minifyString(string jsonString, bool hasComments = false) @trusted
8380 {
8481 in_singleline_comment = true ;
8582 }
83+ else if (notSlashAndNoSpaceOrBreak(matchFrontHit))
84+ {
85+ new_str ~= matchFrontHit;
86+ }
8687 }
8788 else if (in_multiline_comment && ! in_singleline_comment && matchFrontHit == " */" )
8889 {
@@ -93,35 +94,44 @@ string minifyString(string jsonString, bool hasComments = false) @trusted
9394 in_singleline_comment = false ;
9495 }
9596 }
96-
97+ if (! hasComment && notSlashAndNoSpaceOrBreak(matchFrontHit))
98+ {
99+ new_str ~= matchFrontHit;
100+ }
97101 match.popFront();
98102 }
99103 new_str ~= rightContext;
100104 return new_str.array().join(" " );
101105}
102106
103- bool hasNoSlashOrEvenNumberOfSlashes (string leftContext) @safe
107+ private bool hasNoSlashOrEvenNumberOfSlashes (string leftContext) @safe
104108{
105109 auto leftContextMatch = leftContext.matchFirst(repeatingBackSlashRegex);
106110 // if not matched the hit length will be 0 (== leftContextMatch.empty())
107111 return leftContextMatch.hit().length % 2 == 0 ;
108112}
109113
114+ private bool notSlashAndNoSpaceOrBreak (string matchFrontHit)
115+ {
116+ return matchFrontHit != " \" " && matchFrontHit.matchFirst(spaceOrBreakRegex).empty();
117+ }
118+
110119/**
111120 Minify the given files in place. It minifies the files in parallel.
112121
113122 Params:
114123 files = the paths to the files.
124+ hasComment = a boolean to support comments in json. Default: `false`.
115125*/
116- void minifyFiles (string [] files)
126+ void minifyFiles (string [] files, bool hasComment = false )
117127{
118128 import std.parallelism : parallel;
119129 import std.file : readText, write;
120130
121131 foreach (ref file; files.parallel())
122132 {
123133 const string jsonString = readText(file);
124- const minifiedJsonString = minifyString(jsonString);
134+ const minifiedJsonString = minifyString(jsonString, hasComment );
125135 write(file, minifiedJsonString);
126136 }
127137}
0 commit comments