Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8081577

(fmt) java.util.Formatter uppercase variants not using Locale as described

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8u45
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Darwin hostname.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64


      A DESCRIPTION OF THE PROBLEM :
      The Formatter javadoc shows that using the uppercase format variants such as %S should use the locale similar to String.upperCase(), but this doesn't appear to be the case if we use an example such as the Turkish characters stated in the String javadoc where upper/lower case conversion can cause problems.
      This was tested using both variations where you can either specify the Locale within the Formatter constructor, or as the 0th argument to format()

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile test case and run with:

      java -Duser.language=tr STR02 script

      note the 'i' above should be the \u0069 character

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      output:

      Matched SCRIPT in English
      Default formatter is SCRİPT
      English formatter is SCRIPT and tester: SCRIPT
      matched format SCRIPT in English
      Default formatter is SCRIPT
      matched format SCRIPT
      English formatter is SCRIPT and tester: SCRIPT
      matched format SCRIPT in English

      ACTUAL -
      Matched SCRIPT in English
      Default formatter is SCRİPT
      English formatter is SCRİPT and tester: SCRİPT
      Default formatter is SCRİPT
      English formatter is SCRİPT and tester: SCRİPT


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Locale;
      import java.util.Formatter;

      public class STR02 {
          public static void main(String[] args){
              if (args.length < 1){
                  System.out.printf("%s", "Not enough arguments");
                  return;
              }
              processTag(args[0]);
              processEnglishTag(args[0]);
              Formatter formatter = new Formatter();
              processTag(formatter, args[0]);
              Formatter formatter2 = new Formatter();
              processEnglishTag(formatter2, args[0]);
              Formatter formatterEnglish = new Formatter(Locale.ENGLISH);
              processTag(formatterEnglish, args[0]);
              Formatter formatterEnglish2 = new Formatter(Locale.ENGLISH);
              processEnglishTag(formatterEnglish2, args[0]);

          }
          private static void processTag(String tag){
              if (tag.toUpperCase().equals("SCRIPT")){
                  System.out.println("Matched SCRIPT");
                  return;
              }
          }
          private static void processEnglishTag(String tag){
              if (tag.toUpperCase(Locale.ENGLISH).equals("SCRIPT")){
                  System.out.println("Matched SCRIPT in English");
                  return;
              }
          }
          private static void processTag(Formatter formatter, String string){
              Formatter formatter2 = formatter.format("%S", string);
              String tester = formatter2.toString();
              System.out.println("Default formatter is " + formatter2);
              if (tester.equals("SCRIPT")){
                  System.out.println("matched format SCRIPT");
                  return;
              }
          }
          private static void processEnglishTag(Formatter formatter, String string){
              Formatter formatter2 = formatter.format(Locale.ENGLISH, "%S", string);
              String tester = formatter2.toString();
              System.out.println("English formatter is " + formatter2 + " and tester: " + tester);
              if (tester.equals("SCRIPT")){
                  System.out.println("matched format SCRIPT in English");
                  return;
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use the lowercase variant of the format string within Formatter.format, then do:
      formatter.toString().upperCase(Locale)

            aefimov Aleksej Efimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: