< prev index next >

make/jdk/src/classes/build/tools/generateemojidata/GenerateEmojiData.java

Print this page




  50             List<Range> extPictRanges = Files.lines(Paths.get(args[1], "emoji-data.txt"))
  51                 .filter(Predicate.not(l -> l.startsWith("#") || l.isBlank()))
  52                 .filter(l -> l.contains("; Extended_Pictograph"))
  53                 .map(l -> new Range(l.replaceFirst(" .*", "")))
  54                 .sorted()
  55                 .collect(ArrayList<Range>::new,
  56                     (list, r) -> {
  57                         // collapsing consecutive pictographic ranges
  58                         int lastIndex = list.size() - 1;
  59                         if (lastIndex >= 0) {
  60                             Range lastRange = list.get(lastIndex);
  61                             if (lastRange.last + 1 == r.start) {
  62                                 list.set(lastIndex, new Range(lastRange.start, r.last));
  63                                 return;
  64                             }
  65                         }
  66                         list.add(r);
  67                     },
  68                     ArrayList<Range>::addAll);
  69 
  70 
  71             // make the code point conditions
  72             // only very few codepoints below 0x2000 are "emojis", so separate them
  73             // out to generate a fast-path check that can be efficiently inlined
  74             String lowExtPictCodePoints = extPictRanges.stream()
  75                     .takeWhile(r -> r.last < 0x2000)
  76                     .map(r -> rangeToString(r))
  77                     .collect(Collectors.joining(" ||\n", "", ";\n"));
  78 
  79             String highExtPictCodePoints = extPictRanges.stream()
  80                     .dropWhile(r -> r.last < 0x2000)
  81                     .map(r -> rangeToString(r))
  82                     .collect(Collectors.joining(" ||\n", "", ";\n"));


  83 
  84             // Generate EmojiData.java file
  85             Files.write(Paths.get(args[2]),
  86                 Files.lines(Paths.get(args[0]))
  87                     .flatMap(l -> {
  88                         if (l.equals("%%%EXTPICT_LOW%%%")) {
  89                             return Stream.of(lowExtPictCodePoints);
  90                         } else if (l.equals("%%%EXTPICT_HIGH%%%")) {
  91                             return Stream.of(highExtPictCodePoints);
  92                         } else {
  93                             return Stream.of(l);
  94                         }
  95                     })
  96                     .collect(Collectors.toList()),
  97                 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
  98         } catch (IOException e) {
  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) + ")";
 112         }
 113     }
 114 
 115     static int toInt(String hexStr) {
 116         return Integer.parseUnsignedInt(hexStr, 16);
 117     }
 118 
 119     static String toHexString(int cp) {
 120         String ret = Integer.toUnsignedString(cp, 16).toUpperCase();
 121         if (ret.length() < 4) {
 122             ret = "0".repeat(4 - ret.length()) + ret;
 123         }
 124         return ret;
 125     }
 126 
 127     static class Range implements Comparable<Range> {
 128         int start;
 129         int last;
 130 
 131         Range (int start, int last) {




  50             List<Range> extPictRanges = Files.lines(Paths.get(args[1], "emoji-data.txt"))
  51                 .filter(Predicate.not(l -> l.startsWith("#") || l.isBlank()))
  52                 .filter(l -> l.contains("; Extended_Pictograph"))
  53                 .map(l -> new Range(l.replaceFirst(" .*", "")))
  54                 .sorted()
  55                 .collect(ArrayList<Range>::new,
  56                     (list, r) -> {
  57                         // collapsing consecutive pictographic ranges
  58                         int lastIndex = list.size() - 1;
  59                         if (lastIndex >= 0) {
  60                             Range lastRange = list.get(lastIndex);
  61                             if (lastRange.last + 1 == r.start) {
  62                                 list.set(lastIndex, new Range(lastRange.start, r.last));
  63                                 return;
  64                             }
  65                         }
  66                         list.add(r);
  67                     },
  68                     ArrayList<Range>::addAll);
  69 

  70             // make the code point conditions
  71             String extPictCodePoints = extPictRanges.stream()
  72                 .map(r -> {
  73                     if (r.start == r.last) {
  74                         return (" ".repeat(12) + "cp == 0x" + toHexString(r.start));
  75                     } else  if (r.start == r.last - 1) {
  76                         return " ".repeat(12) + "cp == 0x" + toHexString(r.start) + " ||\n" +
  77                                " ".repeat(12) + "cp == 0x" + toHexString(r.last);
  78                     } else {
  79                         return " ".repeat(11) + "(cp >= 0x" + toHexString(r.start) +
  80                                " && cp <= 0x" + toHexString(r.last) + ")";
  81                     }
  82                 })
  83                 .collect(Collectors.joining(" ||\n")) + ";\n";
  84 
  85             // Generate EmojiData.java file
  86             Files.write(Paths.get(args[2]),
  87                 Files.lines(Paths.get(args[0]))
  88                     .flatMap(l -> {
  89                         if (l.equals("%%%EXTPICT%%%")) {
  90                             return Stream.of(extPictCodePoints);


  91                         } else {
  92                             return Stream.of(l);
  93                         }
  94                     })
  95                     .collect(Collectors.toList()),
  96                 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
  97         } catch (IOException e) {
  98             e.printStackTrace();












  99         }
 100     }
 101 
 102     static int toInt(String hexStr) {
 103         return Integer.parseUnsignedInt(hexStr, 16);
 104     }
 105 
 106     static String toHexString(int cp) {
 107         String ret = Integer.toUnsignedString(cp, 16).toUpperCase();
 108         if (ret.length() < 4) {
 109             ret = "0".repeat(4 - ret.length()) + ret;
 110         }
 111         return ret;
 112     }
 113 
 114     static class Range implements Comparable<Range> {
 115         int start;
 116         int last;
 117 
 118         Range (int start, int last) {


< prev index next >