CodePoint

Represents a Unicode code point, which is a numerical value that uniquely identifies a character in the Unicode standard.

A code point code is an integer value in the range 0x0000 to 0x10FFFF (inclusive).

Code points are divided into two categories:

  • Basic (BMP, Basic Multilingual Plane) code points: values from 0x0 to 0xFFFF, which can be represented by a single Char (16-bit value).

  • Supplementary code points: values from 0x10000 to 0x10FFFF, which require a surrogate pair of two Char values.

This value class provides a type-safe representation of Unicode code points and operations for working with them, including conversion to/from characters and surrogate pairs, arithmetic operations, and range creation.

Creating CodePoint instances

Code points can be created in several ways:

val fromCode = CodePoint(0x1F600)                  // from integer code
val fromInt = 0x1F600.toCodePoint() // from Int using extension
val fromChar = 'a'.toCodePoint() // from Char
val emoji = CodePoint.fromSurrogatePair(high, low) // from surrogate pair

Converting to characters

  • Use toSingleChar for basic code points to get a single Char.

  • Use toSurrogatePair for supplementary code points to get the surrogate pair representation.

  • Use toCharArray for both basic and supplementary code point to get an array of either one or two Char values.

  • Use toString to get a string representation of the code point consisting of either one or two Char values.

Arithmetic and ranges

CodePoint supports arithmetic operations and range creation:

val next = codePoint + 1
val distance = codePoint2 - codePoint1
val range = 'a'.toCodePoint()..'z'.toCodePoint()

Since Kotlin

2.5

Throws

if the provided code value is outside the valid range.

Samples

import kotlin.text.unicode.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val validCodePoint = CodePoint(0x1F60B) // 😋
println(validCodePoint) // 😋

// val invalidCodePoint = CodePoint(0x20FFFF) // will fail with IllegalArgumentException 
   //sampleEnd
}

Constructors

Link copied to clipboard
constructor(code: Int)

Returns a CodePoint with the specified code value.

Types

Link copied to clipboard
object Companion
Since Kotlin 2.5

Properties

Link copied to clipboard

Returns the Unicode general category of the Unicode character corresponding to this code point.

Since Kotlin 2.5

Returns the Unicode general category of the Unicode character corresponding to this code point.

Since Kotlin 2.5
Link copied to clipboard
val code: Int

The integer value of the Unicode code point, in the range 0x0..0x10FFFF.

Since Kotlin 2.5
Link copied to clipboard
Link copied to clipboard

Returns true if this is a basic (BMP) code point that can be represented by a single Char.

Since Kotlin 2.5
Link copied to clipboard

Returns true if this is a supplementary code point that can be represented by a surrogate pair of two Char values.

Since Kotlin 2.5
Link copied to clipboard
val size: Int

The number of Char values needed to represent this code point.

Since Kotlin 2.5

Functions

Link copied to clipboard
open operator override fun compareTo(other: CodePoint): Int

Compares this code point with the specified other code point for order.

Since Kotlin 2.5
Link copied to clipboard
infix inline fun <T> Comparable<T>.compareTo(other: T): Int

Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

Since Kotlin 1.6
Link copied to clipboard
inline operator fun dec(): CodePoint

Returns the previous code point by decrementing this code point's value by 1.

Since Kotlin 2.5
Link copied to clipboard

Returns a progression from this value down to and including the specified to value with the step -1.

Since Kotlin 2.5
Link copied to clipboard
inline operator fun inc(): CodePoint

Returns the next code point by incrementing this code point's value by 1.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is defined in Unicode.

Since Kotlin 2.5

Returns true if this character (Unicode code point) is defined in Unicode.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is a digit.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is a digit.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is an ISO control character.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is an ISO control character.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is a letter.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is a letter.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is a letter or digit.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is a letter or digit.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is lower case.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is lower case.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is a title case letter.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is a title case letter.

Since Kotlin 2.5
Link copied to clipboard

Returns true if the Unicode character corresponding to this code point is upper case.

Since Kotlin 2.5

Returns true if the Unicode character corresponding to this code point is upper case.

Since Kotlin 2.5
Link copied to clipboard

Determines whether the Unicode character corresponding to this code point is whitespace.

Since Kotlin 2.5

Determines whether the Unicode character corresponding to this code point is whitespace.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to lower case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5
Link copied to clipboard
operator fun minus(other: Int): CodePoint

Subtracts the specified integer other value from this code point.

Since Kotlin 2.5
operator fun minus(other: CodePoint): Int

Subtracts the other code point from this code point and returns the difference as an Int.

Since Kotlin 2.5
Link copied to clipboard
operator fun plus(other: Int): CodePoint

Adds the specified integer other value to this code point.

Since Kotlin 2.5
Link copied to clipboard
inline operator fun rangeTo(other: CodePoint): CodePointRange

Creates a range from this value to the specified other value.

Since Kotlin 2.5
Link copied to clipboard
operator fun rangeUntil(other: CodePoint): CodePointRange

Creates a range from this value up to but excluding the specified other value.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to title case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to title case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to title case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to a CharArray.

Since Kotlin 2.5
Link copied to clipboard

Converts this basic (BMP) code point to a single Char.

Since Kotlin 2.5
Link copied to clipboard
open override fun toString(): String

Returns a string representation of this code point.

Since Kotlin 2.5
Link copied to clipboard

Converts this supplementary code point to a surrogate pair.

Since Kotlin 2.5
inline fun <T> toSurrogatePair(action: (high: Char, low: Char) -> T): T

Converts this supplementary code point to a surrogate pair and applies the given action to its components.

Since Kotlin 2.5
Link copied to clipboard

Returns a range from this value up to but excluding the specified other value.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5
Link copied to clipboard

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5

Converts this code point to upper case using Unicode mapping rules of the invariant locale.

Since Kotlin 2.5