-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from som-snytt/topic/3
Use dotty style
- Loading branch information
Showing
14 changed files
with
1,285 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (C) 2015-2022 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package com.lightbend.emoji | ||
|
||
import scala.util.Try | ||
|
||
/** The value class representing a codepoint in the Emoji set. | ||
* | ||
* @param codePoint the codepoint representing the Emoji character | ||
*/ | ||
class Emoji private (val codePoint: Int) extends AnyVal: | ||
|
||
/** Returns the sequence of characters (usually surrogate pairs). | ||
*/ | ||
def chars: Array[Char] = Character.toChars(codePoint) | ||
|
||
/** Returns the emoji's Unicode name. | ||
*/ | ||
def name: String = Emoji.name(this.codePoint) | ||
|
||
/** Returns the hexadecimal code. | ||
*/ | ||
def toHexString: String = Emoji.toHexString(this.codePoint) | ||
|
||
/** A convenience method that prepends "0x" before toHexString | ||
*/ | ||
def hex: String = "0x" + toHexString | ||
|
||
/** Returns the emoji as a String. Use this if you want smiley faces. | ||
*/ | ||
override def toString = new String(chars) | ||
|
||
/** This is the singleton object for Emoji. | ||
*/ | ||
object Emoji: | ||
|
||
// Don't want this to conflict with shortCode.emoji | ||
extension (string: String) def codePointEmoji: Emoji = Emoji( | ||
Try(Integer.parseInt(string, 16)).recover { | ||
case e: NumberFormatException => | ||
val stripped = string.replace("0x", "") | ||
Integer.parseInt(stripped, 16) | ||
}.getOrElse { | ||
throw new EmojiNotFound("Cannot parse emoji from hexadecimal string") | ||
} | ||
) | ||
|
||
extension (codePoint: Int) def emoji: Emoji = Emoji(codePoint) | ||
|
||
/** Returns the emoji for the given array of characters, throws EmojiNotFound | ||
*/ | ||
def apply(chars: Array[Char]): Emoji = apply(Character.codePointAt(chars, 0)) | ||
|
||
/** Returns the emoji given a string containing the codepoint. | ||
*/ | ||
def apply(string: String): Emoji = apply(string.codePointAt(0)) | ||
|
||
/** Returns the emoji for this codepoint if found, throws EmojiNotFound otherwise. | ||
*/ | ||
def apply(codePoint: Int): Emoji = new Emoji(validated(codePoint)) | ||
|
||
/** Returns Some(emoji) if found, None otherwise. | ||
*/ | ||
def get(codePoint: Int): Option[Emoji] = | ||
if isEmoji(codePoint) then Some(new Emoji(codePoint)) else None | ||
|
||
/** Returns true if this is a valid Unicode codepoint, false otherwise. | ||
*/ | ||
def isEmoji(codePoint: Int): Boolean = | ||
// there is no codeblock for unicode. | ||
// val block = UnicodeBlock.of(codePoint) | ||
// block == Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS_AND_PICTOGRAPHS || | ||
// block == Character.UnicodeBlock.EMOTICONS || | ||
// block == Character.UnicodeBlock.TRANSPORT_AND_MAP_SYMBOLS || | ||
// block == Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS || | ||
// block == Character.UnicodeBlock.DINGBATS | ||
Character.isValidCodePoint(codePoint) | ||
|
||
/** Throws an exception if this is not a valid Unicode codepoint. | ||
*/ | ||
def validated(codePoint: Int): Int = | ||
if isEmoji(codePoint) then codePoint else throw new EmojiNotFound("Code point is not emoji!") | ||
|
||
/** Returns the unicode name for this codePoint. Throws EmojiNotFound if this is not a valid codepoint. | ||
* | ||
* @param codePoint the codePoint. | ||
* @return the unicode description of the emoji. | ||
*/ | ||
def name(codePoint: Int): String = | ||
Option(Character.getName(codePoint)).getOrElse { | ||
throw new EmojiNotFound("No name found for this codePoint") | ||
} | ||
|
||
/** Returns the hexadecimal string of the code point. | ||
*/ | ||
def toHexString(codePoint: Int): String = codePoint.toHexString | ||
end Emoji | ||
|
||
class EmojiNotFound(msg: String) extends RuntimeException(msg) |
8 changes: 8 additions & 0 deletions
8
src/main/scala-3/com/lightbend/emoji/ScalaVersionSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright (C) 2015-2022 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package com.lightbend.emoji | ||
|
||
object ScalaVersionSpecific: | ||
def checkLengths(sc: StringContext, args: Seq[Any]): Unit = | ||
StringContext.checkLengths(args, sc.parts) |
Oops, something went wrong.