[][src]Trait hangul::HangulExt

Utilities to handle the Hangul syllables.

This trait extends char with methods including:

  • Predicate checks whether given char is a Hangul syllable
  • Predicate indicates whether the syllable has a jongseong — in other words, closed
  • Getters for choseong, jungseong, jongseong, and jamo
  • Iterator iterates over jamos consisting a syllable

To use these methods, add this to your Cargo.toml:

[dependencies]
hangul = "0.1.1"

then import HangulExt trait:

use hangul::HangulExt;

Now you can use the methods.

use hangul::HangulExt;

assert_eq!('京'.is_syllable(), false);
assert_eq!('설'.to_jamo(), Ok(( 'ㅅ', 'ㅓ', Some('ㄹ'))));

Required methods

Loading content...

Implementations on Foreign Types

impl HangulExt for char[src]

fn is_syllable(self) -> bool[src]

Tests whether the char is a Hangul syllable.

Example

use hangul::HangulExt;

assert_eq!('냐'.is_syllable(), true);
assert_eq!('猫'.is_syllable(), false); // 猫 is a Chinese character

fn is_open(self) -> Result<bool, ParseSyllableError>[src]

Tests whether char is an open syllable, i.e., doesn't have jongseong.

Errors

If it's not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('Ж'.is_open(), Err(ParseSyllableError));

Example

use hangul::HangulExt;

assert_eq!('해'.is_open().unwrap(), true); // 해 is open because it doesn't have any jongseong.
assert_eq!('달'.is_open().unwrap(), false); // 달 is open because it has a jongseong ㄹ.

fn is_closed(self) -> Result<bool, ParseSyllableError>[src]

Tests whether char is a closed syllable, i.e., has jongseong.

Errors

If it's not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('א'.is_closed(), Err(ParseSyllableError));

Example

use hangul::HangulExt;

assert_eq!('물'.is_closed().unwrap(), true); // 물 is closed because it has a jongseong ㄹ.
assert_eq!('무'.is_closed().unwrap(), false); // 무 is open because it doesn't have any jongseong.

fn choseong(self) -> Result<char, ParseSyllableError>[src]

Returns compatibility jamo choseong of a given char.

Errors

If it's not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('🥑'.choseong(), Err(ParseSyllableError));

Examples

use hangul::HangulExt;

assert_eq!('항'.choseong().unwrap(), 'ㅎ');

Note that compatibility jamo is different from jamo. This crate only deals with the compatibility jamo.

use hangul::HangulExt;

// Jamo
let p = 'ᄑ';
assert_eq!(p as u32, 0x1111);

// Compatibility jamo
let p_compat = '푸'.choseong().unwrap();
assert_eq!(p_compat, 'ㅍ');
assert_eq!(p_compat as u32, 0x314D);

assert_ne!(p_compat, p);

fn jungseong(self) -> Result<char, ParseSyllableError>[src]

Returns compatibility jamo jungseong of a given char.

Errors

If it's not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('L'.jungseong(), Err(ParseSyllableError));

Examples

use hangul::HangulExt;

assert_eq!('괴'.jungseong().unwrap(), 'ㅚ');

Note that compatibility jamo is different from jamo. This crate only deals with the compatibility jamo.

use hangul::HangulExt;

// Jamo
let u = 'ᅲ';
assert_eq!(u as u32, 0x1172);

// Compatibility jamo
let u_compat = '퓨'.jungseong().unwrap();
assert_eq!(u_compat, 'ㅠ');
assert_eq!(u_compat as u32, 0x3160);

assert_ne!(u_compat, u);

fn jongseong(self) -> Result<Option<char>, ParseSyllableError>[src]

Returns Option of a compatibility jamo jongseong of a given char.

Errors

If it's not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('か'.jongseong(), Err(ParseSyllableError)); // か is a katakana

Examples

use hangul::HangulExt;

assert_eq!('말'.jongseong().unwrap(), Some('ㄹ'));
assert_eq!('소'.jongseong().unwrap(), None);

Note that compatibility jamo is different from jamo. This crate only deals with the compatibility jamo.

use hangul::HangulExt;

// Jamo
let rg = 'ᆰ';
assert_eq!(rg as u32, 0x11B0);

// Compatibility jamo
let rg_compat = '닭'.jongseong().unwrap().unwrap();
assert_eq!(rg_compat, 'ㄺ');
assert_eq!(rg_compat as u32, 0x313A);

assert_ne!(rg_compat, rg);

fn to_jamo(self) -> Result<(char, char, Option<char>), ParseSyllableError>[src]

Examples

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('결'.to_jamo(), Ok(('ㄱ', 'ㅕ', Some('ㄹ'))));
assert_eq!('씨'.to_jamo(), Ok(('ㅆ', 'ㅣ', None)));
assert_eq!('a'.to_jamo(), Err(ParseSyllableError));

fn jamos(self) -> Result<Jamos, ParseSyllableError>[src]

Returns an iterator over the jamos of the syllable.

Errors

If given char is not a syllable, returns ParseSyllableError.

use hangul::{HangulExt, ParseSyllableError};

assert_eq!('K'.jamos(), Err(ParseSyllableError));

Examples

Basic usage:

use hangul::{HangulExt};

let mut jamos = '활'.jamos().unwrap();

assert_eq!(jamos.next(), Some('ㅎ'));
assert_eq!(jamos.next(), Some('ㅘ'));
assert_eq!(jamos.next(), Some('ㄹ'));

assert_eq!(jamos.next(), None);

You can use it with iterator methods like flat_map or fold to decompose Hangul String.

use hangul::{HangulExt};

assert_eq!(
  "첫사랑"
    .chars()
    .flat_map(|c| c.jamos().unwrap())
    .collect::<String>(),
  "ㅊㅓㅅㅅㅏㄹㅏㅇ"
);

assert_eq!(
  "첫사랑은 두 번 다시 겪을 수 없다."
    .chars()
    .fold("".to_owned(), |acc, c| acc
      + &c
        .jamos()
        .map(|j| j.collect::<String>())
        .unwrap_or(c.to_string())),
  "ㅊㅓㅅㅅㅏㄹㅏㅇㅇㅡㄴ ㄷㅜ ㅂㅓㄴ ㄷㅏㅅㅣ ㄱㅕㄲㅇㅡㄹ ㅅㅜ ㅇㅓㅄㄷㅏ."
);
Loading content...

Implementors

Loading content...