@@ -159,6 +159,20 @@ public static List<Edit> addPublicToTopLevelMethods(CompilationUnit cu) {
159159 return edits ;
160160 }
161161
162+
163+ // Verifies that whole input String is floating point literal. Can't be used for searching.
164+ // https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-DecimalFloatingPointLiteral
165+ public static final Pattern FLOATING_POINT_LITERAL_VERIFIER ;
166+ static {
167+ final String DIGITS = "(?:[0-9]|[0-9][0-9_]*[0-9])" ;
168+ final String EXPONENT_PART = "(?:[eE][+-]?" + DIGITS + ")" ;
169+ FLOATING_POINT_LITERAL_VERIFIER = Pattern .compile (
170+ "(?:^" + DIGITS + "\\ ." + DIGITS + "?" + EXPONENT_PART + "?[fFdD]?$)|" +
171+ "(?:^\\ ." + DIGITS + EXPONENT_PART + "?[fFdD]?$)|" +
172+ "(?:^" + DIGITS + EXPONENT_PART + "[fFdD]?$)|" +
173+ "(?:^" + DIGITS + EXPONENT_PART + "?[fFdD]$)" );
174+ }
175+
162176 public static List <Edit > replaceColorAndFixFloats (CompilationUnit cu ) {
163177 final List <Edit > edits = new ArrayList <>();
164178
@@ -175,7 +189,7 @@ public boolean visit(SimpleType node) {
175189 @ Override
176190 public boolean visit (NumberLiteral node ) {
177191 String s = node .getToken ().toLowerCase ();
178- if (! s . endsWith ( "f" ) && !s .endsWith ("d " ) && ( s . contains ( "." ) || s . contains ( "e" ) )) {
192+ if (FLOATING_POINT_LITERAL_VERIFIER . matcher ( s ). matches ( ) && !s .endsWith ("f " ) && ! s . endsWith ( "d" )) {
179193 edits .add (Edit .insert (node .getStartPosition () + node .getLength (), "f" ));
180194 }
181195 return super .visit (node );
@@ -202,6 +216,7 @@ public static List<Edit> replaceColorRegex(CharSequence source) {
202216 return edits ;
203217 }
204218
219+
205220 public static final Pattern NUMBER_LITERAL_REGEX =
206221 Pattern .compile ("[-+]?[0-9]*\\ .?[0-9]+(?:[eE][-+]?[0-9]+)?" );
207222
0 commit comments