【Kotlin】絵文字を含む Unicode 文字列の文字数をカウントする方法と文字ごとの構成要素

絵文字 は AndroidStudio 上で扱いづらいですよね。

例えば、


"😀😍"

という2つの絵文字は、


"\uD83E\uDD78" + "\uD83D\uDC3B\u200D\u2744\uFE0F"

と書く方が編集しやすいです。(しかし分かりづらい。)

絵文字をコピーして Android Studio 上でペーストすると、

\uXXXX\uXXXX

のような「UTF-16 エスケープシーケンス」に置き換わりますよね。

(しかし置き換わらない場合もある。)

👉 Python vs Kotlin Unicode Escape Sequence (エスケープシーケンス) の記述 hatena-bookmark

 

😀 文字を数える

検証するために文字列データを作ります。

絵文字も新旧バージョンのものを含めます。

15文字です。

比較的に新しい Unicode Emoji 15.1 の2つの絵文字は、今現在、まともに見たことがありません。

このデータの文字数をカウントするのは、


println(Regex("\\X").findAll(data).count())
// 15

というかんじでしょうか。

Unicode extended grapheme clusters are supported by the grapheme cluster matcher \X.

👉 2.2 Extended Grapheme Clusters and Character Classes with Strings hatena-bookmark
👉 Unicord support - Pattern  |  Android Developers hatena-bookmark

強く開発環境に影響されると思いますので、各バージョン安定版最新に更新して揃えておくべきでしょう。


android {
  // ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_17.toString()
  }
}

 

😀 文字の構成(要素)を確認する

人間が見ている文字は内部的には複数の要素から構成されています。

絵文字周りで開発している方にはどこかで見たようなすごく便利な表です。

文字列を渡せば簡単に確認できるようにメソッドにしておきます。

先ほどの1文字ごとに分割したあとにその内部を要素に分割して表示します。

data は、先述の15文字を使ってみました。


dump(data)

AndroidStudio のデバッグウィンドウやターミナルでも表示できない絵文字はありますが、内部的には問題なく処理できてるように見えます。

 

😀 まとめ

今回、新しくはっきり認識できたことは、

kotlin の String.length は、


その文字(列)の Char (/uXXXX) の数を表している。

ということ。コードポイントの数ではない。

Char は、コードポイントを 内部的な UTF-16サロゲートペア 分割されたあとの要素。


println("👩‍👧‍👦".length)
// 8

👉 kotlin/Char.kt at 924c28507067cbfbf78a6509ea89eabe496e34ca · JetBrains/kotlin · GitHub hatena-bookmark



👉 絵文字が意図しない白黒で表示される ➡️ - Unicode Variation Selector hatena-bookmark


【Unicode】UTF-16 サロゲートぺア と コードポイント の変換

UTF-16 は、Unicode の文字をコードポイントと呼ばれる整数値で表現するエンコーディング方式です。UTF-16 では、一部のUnicode 文字は16ビットでは表現できないため、サロゲートペアと呼ばれる特殊な方法で符号化されます。

サロゲートペアは、2つの16ビットの値(上位サロゲートと下位サロゲート)を組み合わせて1つの Unicode 文字を表現します。上位サロゲートは 0xD800-0xDBFF の範囲にあり、下位サロゲートは 0xDC00-0xDFFF の範囲にあります。

Wikipedia で調べると数式があります。

【Unicode】UTF-16 サロゲートぺア と コードポイント の変換
👉 Unicode - Wikipedia hatena-bookmark


code-point <-> surrogate-pair

これに合わせて Python コードにしてみました。


def cp_to_sp(cp):
    cp -= 0x10000
    hsg = cp // 0x400 + 0xD800
    lsg = cp % 0x400 + 0xDC00
    return hsg, lsg


def sp_to_cp(hsg, lsg):
    cp = 0x10000 + (hsg - 0xD800) * 0x400 + (lsg - 0xDC00)
    return cp

この関数を、みんな大好き絵文字「👍」で試してみます。


👉 GitHub Emoji Unicode Full Emoji List - shortcode | code point | escape-sequence hatena-bookmark

表にあるデータから、コードポイントとサロゲートペアのそれぞれの値を使います。


cp = 0x1f44d
hsg, lsg = cp_to_sp(cp)
print(hex(hsg), hex(lsg))
# 0xd83d 0xdc4d

hsg = 0xd83d
lsg = 0xdc4d
cp = sp_to_cp(hsg, lsg)
print(hex(cp))
# 0x1f44d

うまくいってます。

 

👍 まとめ

Unicode まわりは CJK のおかげでか、ややこしいことになってます。

UTF-16などのエンコード処理を扱う場合、コード内では「コードポイント」を中心にを進めると幸せになることがよくあります。


👉 Python vs Kotlin Unicode Escape Sequence (エスケープシーケンス) の記述 hatena-bookmark


GitHub Emoji Unicode Full Emoji List - shortcode | code point | escape-sequence

絵文字の「ショートコード」は各プラットフォームごとに異なるので困ったものです。

GitHub でショートコードが用意されている絵文字は、最新の Unicode 絵文字バージョンではないようですが、Facebook、Twitter など大手プラットフォームで直接を生貼りでも問題なく使えるような雰囲気です。

👉 Emojis - GitHub Docs




「GitHub ショートコード」と「Unicode コードポイント」、「Unicode エスケープシーケンス」をスクリプトで一覧にして書き出しておきます。

一番左の「emoji」カラムは絵文字クリックから Emojipedia に飛べます。




👉 Python vs Kotlin Unicode Escape Sequence (エスケープシーケンス) の記述 hatena-bookmark


【ChatGPT】Git comit message (コミットメッセージ) をサクッと簡単に作る macOS ショートカットと OPENAI プロンプト 🤖

Git のコミットメッセージに


- 英語で書くか日本語で書くか
- 絵文字を入れるか入れないか
- prefix を付けるか付けないか

という面倒な話はすべて解決します。

以下の動画のようなショートカットツールです。



 

🤖 プロンプト

ここがポイントです。


Create 10 git commit messages in English for the following changes in the following format. The emoji should be one of the github-shortcode characters that matches your changes.

Format: "emoji :github-shortcode: prefix:commit-message" 
Change Description: "アプリ起動時間のの短縮"

これをベースに会社や上司やプロジェクトの環境によって書き換えればいけます。


- 10
- English
- emoji
- shortcode
- prefix

ここらが変更対象の単語となるでしょう。

 

🤖 ChatGPT API

ChatGPT API を使うには課金登録して OPENAI_API_KEY が必要です。

macOS/iOS ショートカットアプリは、コンポーネントに


- WEB クライアント
- JSON パーサー

の機能を含む「Get Content of URL」という部品があらかじめ用意されています。

しかも、


- リクエストメソッド
- リクエストヘッダー
- リクエストボディ

を GUI からノーコードで設定できるので便利です。

 

🤖 ダウンロード

ショートカットは無料で公開できて、だれでもダウンロードして利用することがきます。


👉 Git Commit Message 😄 hatena-bookmark

[編集] から OPENAI_API_KEY を貼り付けると利用可能になります。

 

🤖 まとめ

ChatGPT API を使うには課金登録が必要ですが、この程度の利用ではいくらコミットしてもジュース1本分の金額には届きません。

実際に使いながら WEB API クライアントのプログラミング実装の基本を学ぶにも最適な教材になると思います。

今回は、厳格なプログラム作成に集中したあとに、ふんわりとしたコミットメッセージ作成はなんとなくつらく思えるので作ってみました。

この記事は、以下のプロジェクトのプロンプトにインスパイアされています。


    Refer to the provided git diff or code snippet and provide a suitable commit message.

    When reviewing the diff or code, focus on identifying the main purpose of the changes.
    Are they fixing a bug, adding a new feature, improving performance or readability, or something else?
    Use this information to craft a concise and detailed gitmoji commit message that clearly describes what the provided code or diff does.

    Describe the change to the best of your capabilities in one short sentence. Don't go into too much detail.

    When reviewing a diff, pay attention to the changed filenames and extract the context of the changes.
    This will help you create a more relevant and informative commit message.
    Here are some examples of how you can interpret some changed filenames:
      - Files or filepaths that reference testing are usually related to tests.
      - Markdown files are usually related to documentation.
      - Config file adjustments are usually related to configuration changes.

    Here is a list of gitmoji codes and their descriptions of what they mean when they are used: """
    ${gitmojis}
    """

    Try to match the generated message to a fitting emoji using its description from the provided list above.
    So go look in the descriptions and find the one that best matches the description.

    Always start your commit message with a gitmoji followed by the message starting with a capital letter.
    Never mention filenames or function names in the message.

    Don't do this:
      - :bug: Fix issue in calculateTotalPrice function
      - :zap: Improve performance of calculateTopProducts function
      - :lipstick: Refactor styling for calculateCartTotal function
      - :memo: Update documentation for getProductById function

    Do this:
      - :bug: Fix issue with shopping cart checkout process
      - :zap: Improve performance of search functionality
      - :lipstick: Refactor styling for product details page
      - :memo: Update documentation for API endpoints

    ${
      context
        ? `
          Refer to the provided additional context to assist you with choosing a correct gitmoji
          and constructing a good message: """
          ${context}
          """
        `
        : ''
    }

    Here is the provided git diff or code snippet: """
    ${prepareDiff(diff, minify)}
    """

👉 Generate your gitmoji commit message 👋 hatena-bookmark



Dependency androidx.activity.* | androidx.core.* requires libraries and applications that depend on it to compile against codename "UpsideDownCake" of the Android APIs.

androidx.* の group = "androidx.activity"group = "androidx.core" などの基本的なモジュールの非 stable 版は、非 stable API では使えないようです。


3 issues were found when checking AAR metadata:

  1.  Dependency 'androidx.activity:activity-ktx:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity-ktx:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  2.  Dependency 'androidx.activity:activity:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  3.  Dependency 'androidx.activity:activity-compose:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity-compose:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

Note: This version will only compile against the Android 14 (Upside Down Cake) Beta 1 SDK.

👉 Activity  |  Jetpack  |  Android Developers hatena-bookmark


2 issues were found when checking AAR metadata:

  1.  Dependency 'androidx.core:core-ktx:1.12.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.core:core-ktx:1.12.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  2.  Dependency 'androidx.core:core:1.12.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.core:core:1.12.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

👉 Core  |  Jetpack  |  Android Developers hatena-bookmark
👉 android - Issues were found when checking AAR metadata: androidx.core:core:1.12.0-alpha01 and androidx.core:core-ktx:1.12.0-alpha01 - Stack Overflow hatena-bookmark

 

🔗 まとめ

コアな Android SDK ライブラリの 非Stable版 は、APIバージョン Stable では使えない。