Skip to content

Commit 6cb4cc0

Browse files
authored
Merge pull request #37 from vorburger/🚀
🚀 (AKA Base256Emoji)
2 parents 4c7c412 + 166578b commit 6cb4cc0

File tree

5 files changed

+247
-118
lines changed

5 files changed

+247
-118
lines changed

pom.xml

+76-75
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,85 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
45

5-
<groupId>com.github.multiformats</groupId>
6-
<artifactId>java-multibase</artifactId>
7-
<version>v1.2.0</version>
8-
<packaging>jar</packaging>
6+
<groupId>com.github.multiformats</groupId>
7+
<artifactId>java-multibase</artifactId>
8+
<version>v1.3.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
910

10-
<name>multibase</name>
11-
<url>https://github.com/multiformats/java-multibase</url>
11+
<name>multibase</name>
12+
<url>https://github.com/multiformats/java-multibase</url>
1213

13-
<issueManagement>
14-
<url>https://github.com/multiformats/java-multibase/issues</url>
15-
<system>GitHub Issues</system>
16-
</issueManagement>
14+
<issueManagement>
15+
<url>https://github.com/multiformats/java-multibase/issues</url>
16+
<system>GitHub Issues</system>
17+
</issueManagement>
1718

18-
<scm>
19-
<url>https://github.com/multiformats/java-multibase</url>
20-
<connection>scm:git:git://github.com/multiformats/java-multibase.git</connection>
21-
<developerConnection>scm:git:[email protected]:multiformats/java-multibase.git</developerConnection>
22-
</scm>
19+
<scm>
20+
<url>https://github.com/multiformats/java-multibase</url>
21+
<connection>scm:git:git://github.com/multiformats/java-multibase.git</connection>
22+
<developerConnection>scm:git:[email protected]:multiformats/java-multibase.git</developerConnection>
23+
</scm>
2324

24-
<licenses>
25-
<license>
26-
<name>MIT License</name>
27-
<url>https://github.com/multiformats/java-multiaddr/blob/master/LICENSE</url>
28-
<distribution>repo</distribution>
29-
</license>
30-
</licenses>
25+
<licenses>
26+
<license>
27+
<name>MIT License</name>
28+
<url>https://github.com/multiformats/java-multiaddr/blob/master/LICENSE</url>
29+
<distribution>repo</distribution>
30+
</license>
31+
</licenses>
3132

32-
<properties>
33-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
35-
<version.junit>5.11.0</version.junit>
36-
<version.hamcrest>3.0</version.hamcrest>
37-
</properties>
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
36+
<version.junit>5.11.0</version.junit>
37+
<version.hamcrest>3.0</version.hamcrest>
38+
</properties>
3839

39-
<dependencies>
40-
<dependency>
41-
<groupId>org.junit.jupiter</groupId>
42-
<artifactId>junit-jupiter</artifactId>
43-
<version>${version.junit}</version>
44-
<scope>test</scope>
45-
</dependency>
46-
<dependency>
47-
<groupId>org.hamcrest</groupId>
48-
<artifactId>hamcrest</artifactId>
49-
<version>${version.hamcrest}</version>
50-
<scope>test</scope>
51-
</dependency>
52-
</dependencies>
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter</artifactId>
44+
<version>${version.junit}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.hamcrest</groupId>
49+
<artifactId>hamcrest</artifactId>
50+
<version>${version.hamcrest}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
5354

54-
<build>
55-
<plugins>
56-
<plugin>
57-
<groupId>org.apache.maven.plugins</groupId>
58-
<artifactId>maven-compiler-plugin</artifactId>
59-
<version>3.1</version>
60-
<configuration>
61-
<source>11</source>
62-
<target>11</target>
63-
</configuration>
64-
</plugin>
65-
<plugin>
66-
<groupId>org.apache.maven.plugins</groupId>
67-
<artifactId>maven-surefire-plugin</artifactId>
68-
<version>3.3.1</version>
69-
</plugin>
70-
<plugin>
71-
<groupId>org.apache.maven.plugins</groupId>
72-
<artifactId>maven-jar-plugin</artifactId>
73-
<version>3.0.2</version>
74-
<configuration>
75-
<archive>
76-
<manifest>
77-
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
78-
</manifest>
79-
</archive>
80-
</configuration>
81-
</plugin>
82-
</plugins>
83-
</build>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>3.1</version>
61+
<configuration>
62+
<source>11</source>
63+
<target>11</target>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-surefire-plugin</artifactId>
69+
<version>3.3.1</version>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-jar-plugin</artifactId>
74+
<version>3.0.2</version>
75+
<configuration>
76+
<archive>
77+
<manifest>
78+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
79+
</manifest>
80+
</archive>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</build>
8485
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.ipfs.multibase;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/*
8+
* Copyright 2025 Michael Vorburger.ch
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
/**
24+
* <a href="https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md">Base256Emoji</a>
25+
* is an encoding mapping each 0-255 byte value to (or from) a specific single Unicode Emoji character.
26+
*
27+
* @author <a href="https://www.vorburger.ch/">Michael Vorburger.ch</a>
28+
*/
29+
public class Base256Emoji {
30+
31+
// from https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md
32+
private static final String[] EMOJIS = {
33+
"🚀", "🪐", "☄", "🛰", "🌌", "🌑", "🌒", "🌓", "🌔", "🌕",
34+
"🌖", "🌗", "🌘", "🌍", "🌏", "🌎", "🐉", "☀", "💻", "🖥",
35+
"💾", "💿", "😂", "❤", "😍", "🤣", "😊", "🙏", "💕", "😭",
36+
"😘", "👍", "😅", "👏", "😁", "🔥", "🥰", "💔", "💖", "💙",
37+
"😢", "🤔", "😆", "🙄", "💪", "😉", "☺", "👌", "🤗", "💜",
38+
"😔", "😎", "😇", "🌹", "🤦", "🎉", "💞", "✌", "✨", "🤷",
39+
"😱", "😌", "🌸", "🙌", "😋", "💗", "💚", "😏", "💛", "🙂",
40+
"💓", "🤩", "😄", "😀", "🖤", "😃", "💯", "🙈", "👇", "🎶",
41+
"😒", "🤭", "❣", "😜", "💋", "👀", "😪", "😑", "💥", "🙋",
42+
"😞", "😩", "😡", "🤪", "👊", "🥳", "😥", "🤤", "👉", "💃",
43+
"😳", "✋", "😚", "😝", "😴", "🌟", "😬", "🙃", "🍀", "🌷",
44+
"😻", "😓", "⭐", "✅", "🥺", "🌈", "😈", "🤘", "💦", "✔",
45+
"😣", "🏃", "💐", "☹", "🎊", "💘", "😠", "☝", "😕", "🌺",
46+
"🎂", "🌻", "😐", "🖕", "💝", "🙊", "😹", "🗣", "💫", "💀",
47+
"👑", "🎵", "🤞", "😛", "🔴", "😤", "🌼", "😫", "⚽", "🤙",
48+
"☕", "🏆", "🤫", "👈", "😮", "🙆", "🍻", "🍃", "🐶", "💁",
49+
"😲", "🌿", "🧡", "🎁", "⚡", "🌞", "🎈", "❌", "✊", "👋",
50+
"😰", "🤨", "😶", "🤝", "🚶", "💰", "🍓", "💢", "🤟", "🙁",
51+
"🚨", "💨", "🤬", "✈", "🎀", "🍺", "🤓", "😙", "💟", "🌱",
52+
"😖", "👶", "🥴", "▶", "➡", "❓", "💎", "💸", "⬇", "😨",
53+
"🌚", "🦋", "😷", "🕺", "⚠", "🙅", "😟", "😵", "👎", "🤲",
54+
"🤠", "🤧", "📌", "🔵", "💅", "🧐", "🐾", "🍒", "😗", "🤑",
55+
"🌊", "🤯", "🐷", "☎", "💧", "😯", "💆", "👆", "🎤", "🙇",
56+
"🍑", "❄", "🌴", "💣", "🐸", "💌", "📍", "🥀", "🤢", "👅",
57+
"💡", "💩", "👐", "📸", "👻", "🤐", "🤮", "🎼", "🥵", "🚩",
58+
"🍎", "🍊", "👼", "💍", "📣", "🥂" };
59+
60+
// TODO Propose adding a Guava dependency to use ImmutableMap instead of this
61+
62+
private static final Map<String, Integer> EMOJI_TO_INDEX;
63+
private static final int MAP_EXPECTED_SIZE = EMOJIS.length;
64+
private static final float MAP_LOAD_FACTOR = 1.0f;
65+
66+
static {
67+
if (EMOJIS.length != 256) {
68+
throw new IllegalStateException("EMOJIS.length must be 256, but is " + EMOJIS.length);
69+
}
70+
71+
Map<String, Integer> mutableMap = new HashMap<>(MAP_EXPECTED_SIZE, MAP_LOAD_FACTOR);
72+
for (int i = 0; i < EMOJIS.length; i++) {
73+
mutableMap.put(EMOJIS[i], i);
74+
}
75+
EMOJI_TO_INDEX = Collections.unmodifiableMap(mutableMap);
76+
}
77+
78+
public static String encode(byte[] in) {
79+
StringBuilder sb = new StringBuilder(in.length);
80+
for (byte b : in) {
81+
sb.append(EMOJIS[b & 0xFF]);
82+
}
83+
return sb.toString();
84+
}
85+
86+
public static byte[] decode(String in) {
87+
int length = in.codePointCount(0, in.length());
88+
byte[] bytes = new byte[length];
89+
90+
for (int i = 0; i < in.codePointCount(0, in.length()); i++) {
91+
int cp = in.codePointAt(in.offsetByCodePoints(0, i));
92+
String emoji = new String(Character.toChars(cp));
93+
Integer index = EMOJI_TO_INDEX.get(emoji);
94+
if (index == null) {
95+
throw new IllegalArgumentException("Unknown Base256Emoji character: " + emoji);
96+
}
97+
bytes[i] = (byte) (index & 0xFF);
98+
}
99+
100+
return bytes;
101+
}
102+
103+
}

0 commit comments

Comments
 (0)