Feature #12831

/\X/ (extended grapheme cluster) can't pass's GraphemeBreakTest

Added by mtsmfm (Fumiaki Matsushima) about 4 years ago. Updated almost 4 years ago.

Target version:


I'm trying to replace Rails's grapheme implementation ( with Ruby's extended grapheme cluster (/X/).

I noticed that Ruby's grapheme cluster can't pass's GraphemeBreakTest.

Following test script will fail on Ruby 2.2/2.3

require 'rubygems'
require 'open-uri'
require 'test/unit'

  if >="2.3.0")

class TestGrapheme < Test::Unit::TestCase
  def test_breaks
    each_line_of_break_tests do |*cols|
      *clusters, comment = *cols
      string = {|c| c.pack("U*") }.join
      assert_equal clusters, string.scan(/\X/).map(&:codepoints), comment

  def each_line_of_break_tests(&block)
    lines = 0
    max_test_lines = 0 # Don't limit below 21, because that's the header of the testfile
    URI.parse("{UNICODE_VERSION}/ucd/auxiliary/GraphemeBreakTest.txt").open do |f|
      until f.eof? || (max_test_lines > 21 && lines > max_test_lines)
        lines += 1
        line = f.gets.chomp!
        next if line.empty? || line.start_with?("#")

        cols, comment = line.split("#")
        # Cluster breaks are represented by ÷
        clusters = cols.split("÷").map { |e| e.strip }.reject { |e| e.empty? }
        clusters = do |cluster|
          # Codepoints within each cluster are separated by ×
          codepoints = cluster.split("×").map { |e| e.strip }.reject { |e| e.empty? }
          # codepoints are in hex in the test suite, pack wants them as integers
 { |codepoint| codepoint.to_i(16) }

        # The tests contain a solitary U+D800 <Non Private Use High
        # Surrogate, First> character, which Ruby does not allow to stand
        # alone in a UTF-8 string. So we'll just skip it.
        next if clusters.flatten.include?(0xd800)

        clusters << comment.strip


I found an issue on Onigmo (
but I couldn't on so I created this ticket.

I'm unfamiliar with grapheme so please tell me if I get something wrong.

Updated by shyouhei (Shyouhei Urabe) about 4 years ago

  • Assignee set to naruse (Yui NARUSE)
  • Status changed from Open to Assigned
  • Tracker changed from Bug to Feature

Updated by shyouhei (Shyouhei Urabe) about 4 years ago

We looked at this issue in todays developer meeting and assigned it to Yui. But no one there had implementation of this. It might need some time. Stay tuned!


Updated by naruse (Yui NARUSE) almost 4 years ago

  • Status changed from Assigned to Closed

Applied in changeset r56949.

Regexp supports Unicoe 9.0.0's \X

  • meta character \X matches Unicode 9.0.0 characters with some workarounds for UTR #51 Unicode Emoji, Version 4.0 emoji zwj sequences. [Feature #12831] [ruby-core:77586]

The term "character" can have many meanings bytes, codepoints, combined
characters, and so on. "grapheme cluster" is highest one of such words,
which means user-perceived characters.
Unicode Standard Annex #29 UNICODE TEXT SEGMENTATION specifies how to
handle grapheme clusters (extended grapheme cluster).
But some specs aren't updated to current situation because Unicode Emoji
is rapidly extended without well definition.
It breaks the precondition of UTR#29 "Grapheme cluster boundaries can be
easily tested by looking at immediately adjacent characters". (the
sentence will be removed in the next version)
Though some of its detail are described in Unicode Technical Report #51
UNICODE EMOJI but it is not merged into UTR#29 yet.

Also available in: Atom PDF