javacommons/utils/javasrc/com/nokia/mj/impl/utils/Formatter.java
changeset 79 2f468c1958d0
parent 76 4ad59aaee882
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
   109      * @param string string to replace at the argument
   109      * @param string string to replace at the argument
   110      * @return same formatter
   110      * @return same formatter
   111      */
   111      */
   112     public Formatter arg(String string)
   112     public Formatter arg(String string)
   113     {
   113     {
   114         // Try to replace with patterns %nU,%n, %U
   114         // Try to replace with patterns %[N...N]n, %nU, %n, %U
   115         if (replace("%" + nextIndex + "U", string) ||
   115         String maxPattern = findMaxPattern();
       
   116         if ((maxPattern != null && replace(maxPattern, string)) ||
       
   117                 replace("%" + nextIndex + "U", string) ||
   116                 replace("%" + nextIndex, string) ||
   118                 replace("%" + nextIndex, string) ||
   117                 replace("%U", string))
   119                 replace("%U", string))
   118         {
   120         {
   119             nextIndex++;
   121             nextIndex++;
   120         }
   122         }
   136      */
   138      */
   137     public Formatter arg(int number)
   139     public Formatter arg(int number)
   138     {
   140     {
   139         String localisedNumber = _formatInteger(number);
   141         String localisedNumber = _formatInteger(number);
   140 
   142 
   141         // Try to replace with patterns %Ln, %nN, %n, %N
   143         // Try to replace with patterns %[N...N]n, %Ln, %nN, %n, %N
   142         if (replace("%" + "L" + nextIndex, localisedNumber) ||
   144         String maxPattern = findMaxPattern();
       
   145         if ((maxPattern != null && replace(maxPattern, localisedNumber)) ||
       
   146                 replace("%" + "L" + nextIndex, localisedNumber) ||
   143                 replace("%" + nextIndex + "N", localisedNumber) ||
   147                 replace("%" + nextIndex + "N", localisedNumber) ||
   144                 replace("%" + nextIndex, localisedNumber) ||
   148                 replace("%" + nextIndex, localisedNumber) ||
   145                 replace("%N", localisedNumber))
   149                 replace("%N", localisedNumber))
   146         {
   150         {
   147             nextIndex++;
   151             nextIndex++;
   165      */
   169      */
   166     public Formatter arg(char ch)
   170     public Formatter arg(char ch)
   167     {
   171     {
   168         String chString = new String(new char[] { ch });
   172         String chString = new String(new char[] { ch });
   169 
   173 
   170         // Try to replace with patterns %nC,%n, %C
   174         // Try to replace with patterns %nC, %n, %C
   171         if (replace("%" + nextIndex + "C", chString) ||
   175         if (replace("%" + nextIndex + "C", chString) ||
   172                 replace("%" + nextIndex, chString) ||
   176                 replace("%" + nextIndex, chString) ||
   173                 replace("%C", chString))
   177                 replace("%C", chString))
   174         {
   178         {
   175             nextIndex++;
   179             nextIndex++;
   370             }
   374             }
   371         }
   375         }
   372         return result;
   376         return result;
   373     }
   377     }
   374 
   378 
       
   379     /**
       
   380      * Finds next %[N...N]n pattern from the replaced field.
       
   381      * Returns found pattern, or null if no pattern was found.
       
   382      */
       
   383     private String findMaxPattern()
       
   384     {
       
   385         String result = null;
       
   386         String startPattern = "%[";
       
   387         String endPattern = "]" + nextIndex;
       
   388         int startIndex = replaced.indexOf(startPattern);
       
   389         if (startIndex >= 0)
       
   390         {
       
   391             int endIndex = replaced.indexOf(endPattern, startIndex);
       
   392             if (endIndex >= 0)
       
   393             {
       
   394                 result = replaced.substring(
       
   395                     startIndex, endIndex + endPattern.length());
       
   396             }
       
   397         }
       
   398         return result;
       
   399     }
       
   400 
   375     /*** ----------------------------- NATIVE ----------------------------- */
   401     /*** ----------------------------- NATIVE ----------------------------- */
   376 
   402 
   377     /**
   403     /**
   378      * Format integer to current locale.
   404      * Format integer to current locale.
   379      *
   405      *