make/jdk/src/classes/build/tools/generateemojidata/GenerateEmojiData.java
changeset 55141 db105c4c5776
parent 55013 8dae495a59e7
equal deleted inserted replaced
55140:d4890c3721be 55141:db105c4c5776
    65                         }
    65                         }
    66                         list.add(r);
    66                         list.add(r);
    67                     },
    67                     },
    68                     ArrayList<Range>::addAll);
    68                     ArrayList<Range>::addAll);
    69 
    69 
       
    70 
    70             // make the code point conditions
    71             // make the code point conditions
    71             String extPictCodePoints = extPictRanges.stream()
    72             // only very few codepoints below 0x2000 are "emojis", so separate them
    72                 .map(r -> {
    73             // out to generate a fast-path check that can be efficiently inlined
    73                     if (r.start == r.last) {
    74             String lowExtPictCodePoints = extPictRanges.stream()
    74                         return (" ".repeat(12) + "cp == 0x" + toHexString(r.start));
    75                     .takeWhile(r -> r.last < 0x2000)
    75                     } else  if (r.start == r.last - 1) {
    76                     .map(r -> rangeToString(r))
    76                         return " ".repeat(12) + "cp == 0x" + toHexString(r.start) + " ||\n" +
    77                     .collect(Collectors.joining(" ||\n", "", ";\n"));
    77                                " ".repeat(12) + "cp == 0x" + toHexString(r.last);
    78 
    78                     } else {
    79             String highExtPictCodePoints = extPictRanges.stream()
    79                         return " ".repeat(11) + "(cp >= 0x" + toHexString(r.start) +
    80                     .dropWhile(r -> r.last < 0x2000)
    80                                " && cp <= 0x" + toHexString(r.last) + ")";
    81                     .map(r -> rangeToString(r))
    81                     }
    82                     .collect(Collectors.joining(" ||\n", "", ";\n"));
    82                 })
       
    83                 .collect(Collectors.joining(" ||\n")) + ";\n";
       
    84 
    83 
    85             // Generate EmojiData.java file
    84             // Generate EmojiData.java file
    86             Files.write(Paths.get(args[2]),
    85             Files.write(Paths.get(args[2]),
    87                 Files.lines(Paths.get(args[0]))
    86                 Files.lines(Paths.get(args[0]))
    88                     .flatMap(l -> {
    87                     .flatMap(l -> {
    89                         if (l.equals("%%%EXTPICT%%%")) {
    88                         if (l.equals("%%%EXTPICT_LOW%%%")) {
    90                             return Stream.of(extPictCodePoints);
    89                             return Stream.of(lowExtPictCodePoints);
       
    90                         } else if (l.equals("%%%EXTPICT_HIGH%%%")) {
       
    91                             return Stream.of(highExtPictCodePoints);
    91                         } else {
    92                         } else {
    92                             return Stream.of(l);
    93                             return Stream.of(l);
    93                         }
    94                         }
    94                     })
    95                     })
    95                     .collect(Collectors.toList()),
    96                     .collect(Collectors.toList()),
    96                 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    97                 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    97         } catch (IOException e) {
    98         } catch (IOException e) {
    98             e.printStackTrace();
    99             e.printStackTrace();
       
   100         }
       
   101     }
       
   102 
       
   103     static String rangeToString(Range r) {
       
   104         if (r.start == r.last) {
       
   105             return (" ".repeat(16) + "cp == 0x" + toHexString(r.start));
       
   106         } else  if (r.start == r.last - 1) {
       
   107             return " ".repeat(16) + "cp == 0x" + toHexString(r.start) + " ||\n" +
       
   108                     " ".repeat(16) + "cp == 0x" + toHexString(r.last);
       
   109         } else {
       
   110             return " ".repeat(15) + "(cp >= 0x" + toHexString(r.start) +
       
   111                     " && cp <= 0x" + toHexString(r.last) + ")";
    99         }
   112         }
   100     }
   113     }
   101 
   114 
   102     static int toInt(String hexStr) {
   115     static int toInt(String hexStr) {
   103         return Integer.parseUnsignedInt(hexStr, 16);
   116         return Integer.parseUnsignedInt(hexStr, 16);