22//#define USE_UDON_LABELS
33
44using System . Collections . Generic ;
5+ using System . IO ;
56using System . Text ;
67using System . Text . RegularExpressions ;
78
@@ -36,7 +37,7 @@ public string GetAssemblyStr(LabelTable labelTable = null)
3637 currentLabelTable = labelTable ;
3738
3839#if ! USE_UDON_LABELS
39- assemblyString = Regex . Replace ( assemblyString , @"(?<whitespace>\s*)(?<labeltype>JUMP_LABEL,|JUMP_IF_FALSE_LABEL,)\s*[[](?<label>[a-zA-Z_\d]+)[\]](?<commentwhitespace>\s*)(?<comment>[#]+\s*[a-zA-Z#\s]+)*" , MatchEval ) ;
40+ assemblyString = ReplaceLabels ( assemblyString , labelTable ) ;
4041#endif
4142
4243 currentLabelTable = null ;
@@ -45,36 +46,43 @@ public string GetAssemblyStr(LabelTable labelTable = null)
4546 }
4647
4748#if ! USE_UDON_LABELS
48- private static string MatchEval ( Match match )
49+ private string ReplaceLabels ( string assemblyString , LabelTable labelTable )
4950 {
50- GroupCollection groupCollection = match . Groups ;
51-
52- string replaceStr = "" ;
51+ StringBuilder newAssemblyBuilder = new StringBuilder ( ) ;
5352
54- replaceStr += groupCollection [ "whitespace" ] . Value ; // Whitespace
55-
56- if ( groupCollection [ "labeltype" ] . Value == "JUMP_LABEL," )
53+ using ( StringReader reader = new StringReader ( assemblyString ) )
5754 {
58- replaceStr += "JUMP, " ;
55+ string currentLine = reader . ReadLine ( ) ;
56+
57+ while ( currentLine != null )
58+ {
59+ string line = currentLine . TrimStart ( ' ' , '\n ' , '\r ' ) ;
60+ if ( line . StartsWith ( "JUMP_LABEL," ) )
61+ {
62+ int startIdx = line . IndexOf ( '[' ) + 1 ;
63+ int endIdx = line . IndexOf ( ']' ) ;
64+ string labelName = line . Substring ( startIdx , endIdx - startIdx ) ;
65+ JumpLabel label = labelTable . GetLabel ( labelName ) ;
66+ newAssemblyBuilder . AppendLine ( " JUMP, " + label . AddresStr ( ) ) ;
67+ }
68+ else if ( line . StartsWith ( "JUMP_IF_FALSE_LABEL," ) )
69+ {
70+ int startIdx = line . IndexOf ( '[' ) + 1 ;
71+ int endIdx = line . IndexOf ( ']' ) ;
72+ string labelName = line . Substring ( startIdx , endIdx - startIdx ) ;
73+ JumpLabel label = labelTable . GetLabel ( labelName ) ;
74+ newAssemblyBuilder . AppendLine ( " JUMP_IF_FALSE, " + label . AddresStr ( ) ) ;
75+ }
76+ else
77+ {
78+ newAssemblyBuilder . AppendLine ( currentLine ) ;
79+ }
80+
81+ currentLine = reader . ReadLine ( ) ;
82+ }
5983 }
60- else if ( groupCollection [ "labeltype" ] . Value == "JUMP_IF_FALSE_LABEL," )
61- {
62- replaceStr += "JUMP_IF_FALSE, " ;
63- }
64-
65- string labelName = groupCollection [ "label" ] . Value ;
66-
67- JumpLabel targetLabel = currentLabelTable . GetLabel ( labelName ) ;
68-
69- replaceStr += targetLabel . AddresStr ( ) ;
70-
71- replaceStr += groupCollection [ "commentwhitespace" ] ;
72-
73- // optional comment
74- if ( groupCollection . Count > 4 )
75- replaceStr += groupCollection [ "comment" ] . Value ;
7684
77- return replaceStr ;
85+ return newAssemblyBuilder . ToString ( ) ;
7886 }
7987#endif
8088
0 commit comments