11import python
22
3+ /** A string constant that looks like it may be used in string formatting operations. */
34library class PossibleAdvancedFormatString extends StrConst {
45 PossibleAdvancedFormatString ( ) { this .getText ( ) .matches ( "%{%}%" ) }
56
@@ -51,6 +52,7 @@ library class PossibleAdvancedFormatString extends StrConst {
5152 predicate isExplicitlyNumbered ( ) { exists ( this .fieldId ( _, _) .toInt ( ) ) }
5253}
5354
55+ /** Holds if there is a sequence of `{` braces in `fmt` of length `len` beginning at index `index`. */
5456predicate brace_sequence ( PossibleAdvancedFormatString fmt , int index , int len ) {
5557 exists ( string text | text = fmt .getText ( ) |
5658 text .charAt ( index ) = "{" and not text .charAt ( index - 1 ) = "{" and len = 1
@@ -61,10 +63,12 @@ predicate brace_sequence(PossibleAdvancedFormatString fmt, int index, int len) {
6163 )
6264}
6365
66+ /** Holds if index `index` in the format string `fmt` contains an escaped `{`. */
6467predicate escaped_brace ( PossibleAdvancedFormatString fmt , int index ) {
6568 exists ( int len | brace_sequence ( fmt , index , len ) | len % 2 = 0 )
6669}
6770
71+ /** Holds if index `index` in the format string `fmt` contains a left curly brace that acts as an escape. */
6872predicate escaping_brace ( PossibleAdvancedFormatString fmt , int index ) {
6973 escaped_brace ( fmt , index + 1 )
7074}
@@ -105,15 +109,18 @@ private predicate advanced_format_call(Call format_expr, PossibleAdvancedFormatS
105109 )
106110}
107111
112+ /** A string constant that has the `format` method applied to it. */
108113class AdvancedFormatString extends PossibleAdvancedFormatString {
109114 AdvancedFormatString ( ) { advanced_format_call ( _, this , _) }
110115}
111116
117+ /** A string formatting operation using the `format` method. */
112118class AdvancedFormattingCall extends Call {
113119 AdvancedFormattingCall ( ) { advanced_format_call ( this , _, _) }
114120
115121 /** Count of the arguments actually provided */
116122 int providedArgCount ( ) { advanced_format_call ( this , _, result ) }
117123
124+ /** Gets a formatting string for this call. */
118125 AdvancedFormatString getAFormat ( ) { advanced_format_call ( this , result , _) }
119126}
0 commit comments