@@ -46,13 +46,15 @@ Vector(3, 4), type:Vector
4646Vector(3.141592653589793, 3.141592653589793), type:Vector
4747Vector(1, 0.0), type:Vector
4848 */
49+ /*
50+ String.trim 移除字符串首尾的所有空白字符
51+ 返回一个新的字符串,原始字符串不会被修改。仅移除首尾空白字符,字符串中间的任何空白字符都会保留
52+ 空白字符定义: 包括空格、制表符(\t)、换行符(\n)等
53+ */
54+
4955List <dynamic > str2Factor (String str) {
5056 List <dynamic > factors = [];
51- // 移除末尾的分号和空格
52- str = str.trim (); // 移除空格
53- if (str.endsWith (';' )) {
54- str = str.substring (0 , str.length - 1 ).trim ();
55- }
57+ str = str.trim ();
5658 // 按逗号分割,但要处理 < > 内的逗号
5759 List <String > parts = [];
5860 StringBuffer currentPart = StringBuffer ();
@@ -66,7 +68,7 @@ List<dynamic> str2Factor(String str) {
6668 } else if (char == '>' ) {
6769 inAngleBrackets = false ;
6870 currentPart.write (char);
69- } else if (char == ', ' && ! inAngleBrackets) {
71+ } else if (char == ' ' && ! inAngleBrackets) {
7072 // 不在尖括号内的逗号作为分隔符
7173 String part = currentPart.toString ().trim ();
7274 if (part.isNotEmpty) {
@@ -87,12 +89,13 @@ List<dynamic> str2Factor(String str) {
8789 part = part.trim ();
8890 if (part.isEmpty) continue ;
8991 if (part.startsWith ('<' ) && part.endsWith ('>' )) {
90- // 字符串类型,去掉尖括号
9192 String content = part.substring (1 , part.length - 1 );
92- if (content.contains (',' )) {
93+ if (content.contains (' ' )) {
94+ //向量
9395 var f = str2Factor (content.trim ());
9496 factors.add (Vector (f[0 ], f[1 ]));
9597 } else {
98+ //标签
9699 factors.add ('#label<${content .trim ()}>' );
97100 }
98101 } else {
@@ -104,7 +107,6 @@ List<dynamic> str2Factor(String str) {
104107 return factors;
105108}
106109
107-
108110// 解析值
109111dynamic _parseValue (String value) {
110112 if (value == '.T' ) return true ;
@@ -128,6 +130,16 @@ dynamic _parseValue(String value) {
128130}
129131
130132
133+ String extractAfter (String input, String ? sign) {
134+ String sign_ = sign?? ' of ' ;
135+ final index = input.indexOf (sign_);
136+ if (index == - 1 ) return '' ;
137+ final startIndex = index + sign_.length;
138+ if (startIndex >= input.length) return '' ;
139+ return input.substring (startIndex);
140+ }
141+
142+
131143GMKStructure goCompiler (String source) {
132144 String source_ = removeComments (source);
133145 GMKStructure structure = GMKStructure .newBlank ();
@@ -138,7 +150,7 @@ GMKStructure goCompiler(String source) {
138150 try {
139151 String label = subStringBetween (line, '@' , ' is ' ).trim ();
140152 String method = subStringBetween (line, ' is ' , ' of ' ).trim ();
141- List <dynamic > factor = str2Factor (subStringBetween (line, ' of ' , '; ' ));
153+ List <dynamic > factor = str2Factor (extractAfter (line, ' of ' ));
142154 structure.addStep (GMKCommand (method, label, factor));
143155 } on RangeError catch (e) {
144156 Exception ('字符串解析错误: 在行中找不到必要的分隔符' );
@@ -186,14 +198,14 @@ String adsorbConstNum(num n) {
186198
187199// 吸附到常向量,以及分量数字
188200String adsorbConstVec (Vector v) {
189- if (v.x== 1.0 && v.y== 0.0 ){
201+ if (v.x == 1.0 && v.y == 0.0 ) {
190202 return '.I' ;
191- } else if (v.x== 0.0 && v.y== 1.0 ){
203+ } else if (v.x == 0.0 && v.y == 1.0 ) {
192204 return '.J' ;
193- } else if (v.x== 0.0 && v.y== 0.0 ){
205+ } else if (v.x == 0.0 && v.y == 0.0 ) {
194206 return '.O' ;
195207 } else {
196- return '<${adsorbConstNum (v .x )}, ${adsorbConstNum (v .y )}>' ;
208+ return '<${adsorbConstNum (v .x )} ${adsorbConstNum (v .y )}>' ;
197209 }
198210}
199211
@@ -202,27 +214,27 @@ String factor2Str(List<dynamic> factor) {
202214 String str = '' ;
203215 for (int i = 0 ; i < factor.length; i++ ) {
204216 dynamic item = factor[i];
205- String division = (i + 1 == factor.length) ? "; " : ', ' ;
217+ String division = (i + 1 == factor.length) ? "" : ' ' ;
206218 switch (item.runtimeType) {
207219 case const (String ):
208220 if (item.startsWith ('#label' )) {
209- str = '$str <${subStringBetween (item , '<' , '>' )}>$division ' ;
221+ str = '$str <${subStringBetween (item , '<' , '>' )}>$division ' ;
210222 } else {
211- str = '$str $item $division ' ;
223+ str = '$str $item $division ' ;
212224 }
213225 case const (Vector ):
214- str = '$str ${adsorbConstVec (item )}$division ' ;
226+ str = '$str ${adsorbConstVec (item )}$division ' ;
215227 case const (double ):
216228 String s = adsorbConstNum (item);
217- str = '$str $s $division ' ;
229+ str = '$str $s $division ' ;
218230 case const (int ):
219231 String s = adsorbConstNum (item);
220- str = '$str $s $division ' ;
232+ str = '$str $s $division ' ;
221233 case const (bool ):
222- str = '$str ${item ? '.T' : '.F' }$division ' ;
234+ str = '$str ${item ? '.T' : '.F' }$division ' ;
223235 default :
224236 String s = item.toString ();
225- str = '$str $s $division ' ;
237+ str = '$str $s $division ' ;
226238 }
227239 }
228240 return str;
0 commit comments