From bb79de2bc605685771d433dc4725911a0139fcad Mon Sep 17 00:00:00 2001 From: JaeSang Yoo Date: Fri, 8 Apr 2022 09:43:08 +0900 Subject: [PATCH 1/5] chore: convert tab indent into 4 spaces For consistent indentation, change mixed indentation into one. As the libhangul and previous code base uses 4 spaces for indentation, convert all tab characters into 4 spaces. Signed-off-by: JaeSang Yoo --- hanjpautomata.c | 338 +++++++++++++-------------- hanjpbuffer.c | 94 ++++---- hanjpinputcontext.c | 6 +- hanjpkeyboard.c | 6 +- hanjpunicode.h | 2 +- test/test_gobj_construct_dispose.cpp | 112 ++++----- test/test_input_context_convert.cpp | 18 +- 7 files changed, 288 insertions(+), 288 deletions(-) diff --git a/hanjpautomata.c b/hanjpautomata.c index aa7c453..16ecfb2 100644 --- a/hanjpautomata.c +++ b/hanjpautomata.c @@ -121,155 +121,155 @@ G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_am_base, G_TYPE_OBJECT) static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) { - gint ret = 0; // In normal indexing, it returns 0 - - *diacrit = 0; - // Select row index and set adjuster - switch (cho) { - case 0: // VOID - *diacrit = -1; - case HANGUL_CHOSEONG_IEUNG: // ㅇ - *conso_idx = KANA_CONSONANT__; - break; - case HANGUL_CHOSEONG_KIYEOK: // ㄱ - *diacrit = 1; - case HANGUL_CHOSEONG_KHIEUKH: // ㅋ - case HANGUL_CHOSEONG_SSANGKIYEOK: // ㄲ - *conso_idx = KANA_CONSONANT_K; - break; - case HANGUL_CHOSEONG_CIEUC: // ㅈ - *diacrit = 1; - case HANGUL_CHOSEONG_SIOS: // ㅅ - case HANGUL_CHOSEONG_SSANGSIOS: // ㅆ - *conso_idx = KANA_CONSONANT_S; - break; - case HANGUL_CHOSEONG_TIKEUT: // ㄷ - *diacrit = 1; - case HANGUL_CHOSEONG_SSANGTIKEUT: // ㄸ - case HANGUL_CHOSEONG_CHIEUCH: // ㅊ - case HANGUL_CHOSEONG_THIEUTH: // ㅌ - *conso_idx = KANA_CONSONANT_T; - break; - case HANGUL_CHOSEONG_NIEUN: // ㄴ - *conso_idx = KANA_CONSONANT_N; - break; - case HANGUL_CHOSEONG_PHIEUPH: // ㅍ - case HANGUL_CHOSEONG_SSANGPIEUP: // ㅃ - *diacrit = 1; - case HANGUL_CHOSEONG_PIEUP: // ㅂ - *diacrit += 1; - case HANGUL_CHOSEONG_HIEUH: // ㅎ - *conso_idx = KANA_CONSONANT_H; - break; - case HANGUL_CHOSEONG_MIEUM: // ㅁ - *conso_idx = KANA_CONSONANT_M; - break; - case HANGUL_CHOSEONG_RIEUL: // ㄹ - *conso_idx = KANA_CONSONANT_R; - break; - case HANGUL_CHOSEONG_SSANGNIEUN: - default: - ret = -1; - } - return ret; + gint ret = 0; // In normal indexing, it returns 0 + + *diacrit = 0; + // Select row index and set adjuster + switch (cho) { + case 0: // VOID + *diacrit = -1; + case HANGUL_CHOSEONG_IEUNG: // ㅇ + *conso_idx = KANA_CONSONANT__; + break; + case HANGUL_CHOSEONG_KIYEOK: // ㄱ + *diacrit = 1; + case HANGUL_CHOSEONG_KHIEUKH: // ㅋ + case HANGUL_CHOSEONG_SSANGKIYEOK: // ㄲ + *conso_idx = KANA_CONSONANT_K; + break; + case HANGUL_CHOSEONG_CIEUC: // ㅈ + *diacrit = 1; + case HANGUL_CHOSEONG_SIOS: // ㅅ + case HANGUL_CHOSEONG_SSANGSIOS: // ㅆ + *conso_idx = KANA_CONSONANT_S; + break; + case HANGUL_CHOSEONG_TIKEUT: // ㄷ + *diacrit = 1; + case HANGUL_CHOSEONG_SSANGTIKEUT: // ㄸ + case HANGUL_CHOSEONG_CHIEUCH: // ㅊ + case HANGUL_CHOSEONG_THIEUTH: // ㅌ + *conso_idx = KANA_CONSONANT_T; + break; + case HANGUL_CHOSEONG_NIEUN: // ㄴ + *conso_idx = KANA_CONSONANT_N; + break; + case HANGUL_CHOSEONG_PHIEUPH: // ㅍ + case HANGUL_CHOSEONG_SSANGPIEUP: // ㅃ + *diacrit = 1; + case HANGUL_CHOSEONG_PIEUP: // ㅂ + *diacrit += 1; + case HANGUL_CHOSEONG_HIEUH: // ㅎ + *conso_idx = KANA_CONSONANT_H; + break; + case HANGUL_CHOSEONG_MIEUM: // ㅁ + *conso_idx = KANA_CONSONANT_M; + break; + case HANGUL_CHOSEONG_RIEUL: // ㄹ + *conso_idx = KANA_CONSONANT_R; + break; + case HANGUL_CHOSEONG_SSANGNIEUN: + default: + ret = -1; + } + return ret; } static void divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) { - switch (buffer->jung) { - case HANGUL_JUNGSEONG_WA: - if(*conso_idx == KANA_CONSONANT__) { - *conso_idx = KANA_CONSONANT_W; - } - else { - buffer->jung = HANGUL_JUNGSEONG_O; - buffer->jung2 = HANGUL_JUNGSEONG_A; - } - break; - case HANGUL_JUNGSEONG_YA: - case HANGUL_JUNGSEONG_YU: - case HANGUL_JUNGSEONG_YO: - case HANGUL_JUNGSEONG_YEO: - if(*conso_idx == KANA_CONSONANT__) { - *conso_idx = KANA_CONSONANT_Y; - } - else{ - // Insert Jungseong_I before existing jungseong - buffer->jung2 = buffer->jung; - buffer->jung = HANGUL_JUNGSEONG_I; - } - break; - case HANGUL_JUNGSEONG_YE: - case HANGUL_JUNGSEONG_YAE: - buffer->jung = HANGUL_JUNGSEONG_I; - buffer->jung2 = HANGUL_JUNGSEONG_E; - break; - } + switch (buffer->jung) { + case HANGUL_JUNGSEONG_WA: + if(*conso_idx == KANA_CONSONANT__) { + *conso_idx = KANA_CONSONANT_W; + } + else { + buffer->jung = HANGUL_JUNGSEONG_O; + buffer->jung2 = HANGUL_JUNGSEONG_A; + } + break; + case HANGUL_JUNGSEONG_YA: + case HANGUL_JUNGSEONG_YU: + case HANGUL_JUNGSEONG_YO: + case HANGUL_JUNGSEONG_YEO: + if(*conso_idx == KANA_CONSONANT__) { + *conso_idx = KANA_CONSONANT_Y; + } + else{ + // Insert Jungseong_I before existing jungseong + buffer->jung2 = buffer->jung; + buffer->jung = HANGUL_JUNGSEONG_I; + } + break; + case HANGUL_JUNGSEONG_YE: + case HANGUL_JUNGSEONG_YAE: + buffer->jung = HANGUL_JUNGSEONG_I; + buffer->jung2 = HANGUL_JUNGSEONG_E; + break; + } } static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { - gint ret = 0; // In normal indexing, it returns 0 - - switch(jung) { - case HANGUL_JUNGSEONG_WA: - case HANGUL_JUNGSEONG_YA: - case HANGUL_JUNGSEONG_A: - *vowel_idx = KANA_VOWEL_A; - break; - case HANGUL_JUNGSEONG_I: - *vowel_idx = KANA_VOWEL_I; - break; - case HANGUL_JUNGSEONG_YU: - case HANGUL_JUNGSEONG_EU: - case HANGUL_JUNGSEONG_U: - case 0: - *vowel_idx = KANA_VOWEL_U; - break; - case HANGUL_JUNGSEONG_AE: - case HANGUL_JUNGSEONG_E: - *vowel_idx = KANA_VOWEL_E; - break; - case HANGUL_JUNGSEONG_YO: - case HANGUL_JUNGSEONG_YEO: - case HANGUL_JUNGSEONG_EO: - case HANGUL_JUNGSEONG_O: - *vowel_idx = KANA_VOWEL_O; - break; - default: - ret = -1; - } - return ret; + gint ret = 0; // In normal indexing, it returns 0 + + switch(jung) { + case HANGUL_JUNGSEONG_WA: + case HANGUL_JUNGSEONG_YA: + case HANGUL_JUNGSEONG_A: + *vowel_idx = KANA_VOWEL_A; + break; + case HANGUL_JUNGSEONG_I: + *vowel_idx = KANA_VOWEL_I; + break; + case HANGUL_JUNGSEONG_YU: + case HANGUL_JUNGSEONG_EU: + case HANGUL_JUNGSEONG_U: + case 0: + *vowel_idx = KANA_VOWEL_U; + break; + case HANGUL_JUNGSEONG_AE: + case HANGUL_JUNGSEONG_E: + *vowel_idx = KANA_VOWEL_E; + break; + case HANGUL_JUNGSEONG_YO: + case HANGUL_JUNGSEONG_YEO: + case HANGUL_JUNGSEONG_EO: + case HANGUL_JUNGSEONG_O: + *vowel_idx = KANA_VOWEL_O; + break; + default: + ret = -1; + } + return ret; } static gint jongseong_to_kana(gunichar jong, gunichar *kana) { - gint ret = 0; // In normal conversion, it returns 0 - - switch (jong) { - case HANGUL_JONGSEONG_KIYEOK: - case HANGUL_JONGSEONG_SSANGKIYEOK: - case HANGUL_JONGSEONG_SIOS: - case HANGUL_JONGSEONG_SSANGSIOS: - case HANGUL_JONGSEONG_KHIEUKH: - *kana = KANA_SMALL_TU; - break; - case HANGUL_JONGSEONG_NIEUN: - case HANGUL_JONGSEONG_MIEUM: - case HANGUL_JONGSEONG_PIEUP: - case HANGUL_JONGSEONG_PHIEUPH: - case HANGUL_JONGSEONG_IEUNG: - *kana = KANA_NN; - break; - case 0: - *kana = 0; - break; - default: - ret = -1; - } - return ret; + gint ret = 0; // In normal conversion, it returns 0 + + switch (jong) { + case HANGUL_JONGSEONG_KIYEOK: + case HANGUL_JONGSEONG_SSANGKIYEOK: + case HANGUL_JONGSEONG_SIOS: + case HANGUL_JONGSEONG_SSANGSIOS: + case HANGUL_JONGSEONG_KHIEUKH: + *kana = KANA_SMALL_TU; + break; + case HANGUL_JONGSEONG_NIEUN: + case HANGUL_JONGSEONG_MIEUM: + case HANGUL_JONGSEONG_PIEUP: + case HANGUL_JONGSEONG_PHIEUPH: + case HANGUL_JONGSEONG_IEUNG: + *kana = KANA_NN; + break; + case 0: + *kana = 0; + break; + default: + ret = -1; + } + return ret; } static gint @@ -277,7 +277,7 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { gint i, r = 0; gint conso_idx, vowel_idx, diacrit; - gint conv_err; + gint conv_err; JungBox jungkey; gunichar jamo, kana; gunichar *comb_jungseong; @@ -286,8 +286,8 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); // Check buffer - if (!hanjp_buffer_is_valid(buffer)) { - r = hanjp_buffer_copy_jamoes(buffer, dest); + if (!hanjp_buffer_is_valid(buffer)) { + r = hanjp_buffer_copy_jamoes(buffer, dest); return -r; } @@ -304,43 +304,43 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) //eat Choseong and Jungseong while(buffer->cho || buffer->jung || buffer->jung2) { - // Convert choseong into kana indexing - jamo = hanjp_buffer_pop_choseong(buffer); - conv_err = choseong_to_kana_index(jamo, &conso_idx, &diacrit); - if (conv_err && jamo == HANGUL_CHOSEONG_SSANGNIEUN) { - // it directly converts into KANA_NN - g_array_append_val(dest, KANA_NN); - r++; - continue; - } else if (conv_err) { - r += hanjp_buffer_copy_jamoes(buffer, dest); - return -r; - } + // Convert choseong into kana indexing + jamo = hanjp_buffer_pop_choseong(buffer); + conv_err = choseong_to_kana_index(jamo, &conso_idx, &diacrit); + if (conv_err && jamo == HANGUL_CHOSEONG_SSANGNIEUN) { + // it directly converts into KANA_NN + g_array_append_val(dest, KANA_NN); + r++; + continue; + } else if (conv_err) { + r += hanjp_buffer_copy_jamoes(buffer, dest); + return -r; + } // try reducing double Jungseong to single character - hanjp_buffer_align_jungseong(buffer); + hanjp_buffer_align_jungseong(buffer); if (buffer->jung2 != 0) { jungkey.jung = buffer->jung; jungkey.jung2 = buffer->jung2; comb_jungseong = (gunichar *)g_hash_table_lookup(priv->combine_table, &jungkey.value); if(comb_jungseong != NULL) { buffer->jung = *comb_jungseong; - buffer->jung2 = 0; + buffer->jung2 = 0; } } // Divide Jungseong into eatable - divide_jungseong(buffer, &conso_idx); - + divide_jungseong(buffer, &conso_idx); + //select column index jamo = hanjp_buffer_pop_jungseong(buffer); // victim - conv_err = jungseong_to_kana_index(jamo, &vowel_idx); - if (conv_err) { - r += hanjp_buffer_copy_jamoes(buffer, dest); - return -r; - } - - // indexing done, convert into kana + conv_err = jungseong_to_kana_index(jamo, &vowel_idx); + if (conv_err) { + r += hanjp_buffer_copy_jamoes(buffer, dest); + return -r; + } + + // indexing done, convert into kana kana = KANA_TABLE[conso_idx][vowel_idx] + diacrit; g_array_append_val(dest, kana); r++; @@ -348,15 +348,15 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) // eat jongseong jamo = hanjp_buffer_pop_jongseong(buffer); - conv_err = jongseong_to_kana(jamo, &kana); - if (conv_err) { - // promote jongseong into choseong and retry convert + conv_err = jongseong_to_kana(jamo, &kana); + if (conv_err) { + // promote jongseong into choseong and retry convert jamo = hangul_jongseong_to_choseong(jamo); - hanjp_buffer_push(buffer, jamo); + hanjp_buffer_push(buffer, jamo); r += hanjp_am_base_to_kana(am, dest, buffer); } - else if (jamo != 0) { - // When jamo were not empty, it may converted + else if (jamo != 0) { + // When jamo were not empty, it may converted g_array_append_val(dest, kana); r++; } @@ -375,7 +375,7 @@ hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); - hanjp_buffer_align_jungseong(&priv->buffer); + hanjp_buffer_align_jungseong(&priv->buffer); if(priv->buffer.jung != 0 && priv->buffer.jung2 != 0) { jungkey.jung = priv->buffer.jung; @@ -427,7 +427,7 @@ hanjp_am_base_init(HanjpAutomataBase *am) HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(am); - hanjp_buffer_flush(&priv->buffer); + hanjp_buffer_flush(&priv->buffer); priv->combine_table = g_hash_table_new(g_int64_hash, g_int64_equal); } diff --git a/hanjpbuffer.c b/hanjpbuffer.c index 4f94e79..1940042 100644 --- a/hanjpbuffer.c +++ b/hanjpbuffer.c @@ -7,28 +7,28 @@ extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); static const int BUFF_SIZE = - (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); + (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); /* HanjpBuffer Implementation */ gunichar hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { if ((hangul_is_choseong(ch)) && (buffer->cho == 0)) { buffer->cho = ch; - return 0; + return 0; } else if (hangul_is_jungseong(ch)) { if(buffer->jung == 0) { buffer->jung = ch; - return 0; + return 0; } else if(buffer->jung2 == 0) { buffer->jung2 = ch; - return 0; + return 0; } } else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) { buffer->jong = ch; - return 0; + return 0; } return ch; @@ -36,7 +36,7 @@ hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { gint hanjp_buffer_peek(HanjpBuffer *buffer) { - int i; + int i; for (i = 0; i < BUFF_SIZE; i++) { if (buffer->stack[i]) { @@ -44,17 +44,17 @@ hanjp_buffer_peek(HanjpBuffer *buffer) { } } - return 0; + return 0; } gunichar hanjp_buffer_pop(HanjpBuffer *buffer) { - gint ret = 0; + gint ret = 0; int i; for (i = BUFF_SIZE-1; i > -1; i--) { if (buffer->stack[i]) { - ret = buffer->stack[i]; + ret = buffer->stack[i]; buffer->stack[i] = 0; break; } @@ -73,73 +73,73 @@ hanjp_buffer_flush(HanjpBuffer *buffer) { bool hanjp_buffer_is_valid(HanjpBuffer *buffer) { - int i; + int i; - for (i = 0; i < BUFF_SIZE; i++) { - if (buffer->stack[i] && !hangul_is_jamo(buffer->stack[i])) { - return false; - } - } + for (i = 0; i < BUFF_SIZE; i++) { + if (buffer->stack[i] && !hangul_is_jamo(buffer->stack[i])) { + return false; + } + } - return true; + return true; } gint hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) { - int i; - int copy_cnt = 0; - - for (i = 0; i < BUFF_SIZE; i++) { - if (buffer->stack[i]) { - g_array_append_val(arr, buffer->stack[i]); - copy_cnt += 1; - } - } - return copy_cnt; + int i; + int copy_cnt = 0; + + for (i = 0; i < BUFF_SIZE; i++) { + if (buffer->stack[i]) { + g_array_append_val(arr, buffer->stack[i]); + copy_cnt += 1; + } + } + return copy_cnt; } void hanjp_buffer_clear_filler(HanjpBuffer *buffer) { - int i; - - for (i = 0; i < BUFF_SIZE; i++) { - if ((buffer->stack[i] == HANGUL_CHOSEONG_FILLER) - || (buffer->stack[i] == HANGUL_JUNGSEONG_FILLER)) { - buffer->stack[i] = 0; - } - } + int i; + + for (i = 0; i < BUFF_SIZE; i++) { + if ((buffer->stack[i] == HANGUL_CHOSEONG_FILLER) + || (buffer->stack[i] == HANGUL_JUNGSEONG_FILLER)) { + buffer->stack[i] = 0; + } + } } void hanjp_buffer_align_jungseong(HanjpBuffer *buffer) { - if (buffer->jung == 0) { - buffer->jung = buffer-> jung2; - buffer->jung2 = 0; - } + if (buffer->jung == 0) { + buffer->jung = buffer-> jung2; + buffer->jung2 = 0; + } } gunichar hanjp_buffer_pop_choseong(HanjpBuffer *buffer) { - gunichar ch = buffer->cho; - buffer->cho = 0; + gunichar ch = buffer->cho; + buffer->cho = 0; - return ch; + return ch; } gunichar hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) { - gunichar ch = buffer->jung; - buffer->jung = 0; + gunichar ch = buffer->jung; + buffer->jung = 0; - return ch; + return ch; } gunichar hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) { - gunichar ch = buffer->jong; - buffer->jong = 0; + gunichar ch = buffer->jong; + buffer->jong = 0; - return ch; + return ch; } /**/ diff --git a/hanjpinputcontext.c b/hanjpinputcontext.c index a7bb9ff..a6a5b67 100644 --- a/hanjpinputcontext.c +++ b/hanjpinputcontext.c @@ -36,11 +36,11 @@ hanjp_ic_dispose(GObject *self) g_clear_object(&priv->cur_am); g_clear_object(&priv->keyboard); g_array_unref(priv->preedit); - priv->preedit = NULL; + priv->preedit = NULL; g_array_unref(priv->committed); - priv->committed = NULL; + priv->committed = NULL; g_array_unref(priv->hangul); - priv->hangul = NULL; + priv->hangul = NULL; //chain up G_OBJECT_CLASS(hanjp_ic_parent_class)->dispose(self); diff --git a/hanjpkeyboard.c b/hanjpkeyboard.c index c4b2878..3bba2a7 100644 --- a/hanjpkeyboard.c +++ b/hanjpkeyboard.c @@ -8,7 +8,7 @@ G_DEFINE_INTERFACE(HanjpKeyboard, hanjp_kb, G_TYPE_OBJECT) static void hanjp_kb_default_init(HanjpKeyboardInterface *iface) { - // Nothing to do + // Nothing to do } gunichar hanjp_kb_get_mapping(HanjpKeyboard* self, gint tableid, gint ascii) @@ -42,9 +42,9 @@ hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(HANJP_KB_BUILTIN(self)); - g_return_if_fail(priv->keyboard != NULL); + g_return_if_fail(priv->keyboard != NULL); - return hangul_keyboard_get_mapping(priv->keyboard, tableid, ascii); + return hangul_keyboard_get_mapping(priv->keyboard, tableid, ascii); } static void diff --git a/hanjpunicode.h b/hanjpunicode.h index b1b93b4..7801b48 100644 --- a/hanjpunicode.h +++ b/hanjpunicode.h @@ -5,7 +5,7 @@ // syllable leading consonants Jamo (초성) enum { - // modern Hangul leading consonants + // modern Hangul leading consonants HANGUL_CHOSEONG_KIYEOK = 0x1100, HANGUL_CHOSEONG_SSANGKIYEOK, HANGUL_CHOSEONG_NIEUN, diff --git a/test/test_gobj_construct_dispose.cpp b/test/test_gobj_construct_dispose.cpp index 86a73b3..61b9244 100644 --- a/test/test_gobj_construct_dispose.cpp +++ b/test/test_gobj_construct_dispose.cpp @@ -5,79 +5,79 @@ TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) { - // Given HanjpAutomataDefault object - HanjpAutomataBuiltin *amBuiltin = NULL; - const char* strExpectObjType = "HanjpAutomataBuiltin"; - const char* strActualObjType = NULL; - - // When constructed - amBuiltin = hanjp_am_builtin_new(); - - // Then, object pointer must not be null - ASSERT_NE(amBuiltin, nullptr) - << "HanjpAutomataBuiltin construct failed"; - - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(amBuiltin); - ASSERT_STREQ(strActualObjType, strExpectObjType); - - // When destructed - g_object_unref(amBuiltin); - - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(amBuiltin)) - << "HanjpAutomataBuiltin destruct failed"; + // Given HanjpAutomataDefault object + HanjpAutomataBuiltin *amBuiltin = NULL; + const char* strExpectObjType = "HanjpAutomataBuiltin"; + const char* strActualObjType = NULL; + + // When constructed + amBuiltin = hanjp_am_builtin_new(); + + // Then, object pointer must not be null + ASSERT_NE(amBuiltin, nullptr) + << "HanjpAutomataBuiltin construct failed"; + + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(amBuiltin); + ASSERT_STREQ(strActualObjType, strExpectObjType); + + // When destructed + g_object_unref(amBuiltin); + + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(amBuiltin)) + << "HanjpAutomataBuiltin destruct failed"; } TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) { - // Given HanjpKeyboardDefault object - HanjpKeyboardBuiltin *kbBuiltin = NULL; - const char* strExpectObjType = "HanjpKeyboardBuiltin"; - const char* strActualObjType = NULL; + // Given HanjpKeyboardDefault object + HanjpKeyboardBuiltin *kbBuiltin = NULL; + const char* strExpectObjType = "HanjpKeyboardBuiltin"; + const char* strActualObjType = NULL; - // When constructed - kbBuiltin = hanjp_kb_builtin_new(); + // When constructed + kbBuiltin = hanjp_kb_builtin_new(); - // Then, object pointer must not be null - ASSERT_NE(kbBuiltin, nullptr) - << "HanjpKeyboardBuiltin construct failed"; + // Then, object pointer must not be null + ASSERT_NE(kbBuiltin, nullptr) + << "HanjpKeyboardBuiltin construct failed"; - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(kbBuiltin); - ASSERT_STREQ(strActualObjType, strExpectObjType); + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(kbBuiltin); + ASSERT_STREQ(strActualObjType, strExpectObjType); - // When destructed - g_object_unref(kbBuiltin); + // When destructed + g_object_unref(kbBuiltin); - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(kbBuiltin)) - << "HanjpKeyboardBuiltin destruct failed"; + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(kbBuiltin)) + << "HanjpKeyboardBuiltin destruct failed"; } TEST(GObjectConstructDisposeTest, InputContextConstructDispose) { - // Given HanjpInputContext object - HanjpInputContext *ic = NULL; - const char* strExpectObjType = "HanjpInputContext"; - const char* strActualObjType = NULL; + // Given HanjpInputContext object + HanjpInputContext *ic = NULL; + const char* strExpectObjType = "HanjpInputContext"; + const char* strActualObjType = NULL; - // When constructed - ic = hanjp_ic_new(); + // When constructed + ic = hanjp_ic_new(); - // Then, object pointer must not be null - ASSERT_NE(ic, nullptr) - << "HanjpInputContext construct failed"; + // Then, object pointer must not be null + ASSERT_NE(ic, nullptr) + << "HanjpInputContext construct failed"; - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(ic); - ASSERT_STREQ(strActualObjType, strExpectObjType); + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(ic); + ASSERT_STREQ(strActualObjType, strExpectObjType); - // When destructed - g_object_unref(ic); + // When destructed + g_object_unref(ic); - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(ic)) - << "HanjpInputContext destruct failed"; + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(ic)) + << "HanjpInputContext destruct failed"; } diff --git a/test/test_input_context_convert.cpp b/test/test_input_context_convert.cpp index 9fb9e2b..bf7ba9f 100644 --- a/test/test_input_context_convert.cpp +++ b/test/test_input_context_convert.cpp @@ -3,13 +3,13 @@ class InputContextTest : public ::testing::Test { protected: - void SetUp() override { - // Create context object for each tests + void SetUp() override { + // Create context object for each tests context = hanjp_ic_new(); } void TearDown() override { - // Destruct context object when each tests were done + // Destruct context object when each tests were done g_object_unref(context); } @@ -18,23 +18,23 @@ class InputContextTest : public ::testing::Test { TEST_F(InputContextTest, IC_Hajimemashite) { const char *strGivenKeyInput = "gkwlapaktlxp."; - const char *strExpectJapanese = "はじめまして."; - const char *strActualJapanese = NULL; + const char *strExpectJapanese = "はじめまして."; + const char *strActualJapanese = NULL; - // Given Hanjp Input context + // Given Hanjp Input context GArray *committed = hanjp_ic_ref_commit_string(context); - // When given key input were processed + // When given key input were processed while(*strGivenKeyInput) { hanjp_ic_process(context, (gint)(*strGivenKeyInput)); strGivenKeyInput++; } strActualJapanese = g_ucs4_to_utf8((const gunichar*)committed->data, committed->len, NULL, NULL, NULL); - // Then, converted japanese must be same as expected + // Then, converted japanese must be same as expected EXPECT_STREQ(strActualJapanese, strExpectJapanese); - // Destruct temporal variable in test + // Destruct temporal variable in test g_array_unref(committed); } From 9b76bfe20e37f1f9fca9c113bd4ed2fc9d32b315 Mon Sep 17 00:00:00 2001 From: JaeSang Yoo Date: Fri, 8 Apr 2022 10:44:24 +0900 Subject: [PATCH 2/5] chore: format code blocks 1. Bracket ('{') rules * Functions, structure, union, class, enums Opening and closing brackets are placed on separate line. * Control-flow statements (ex. if, for, while...) Opening bracket should be in same line. If-else like code blocks, closing bracket can be in same line. (ex. "} else {") 2. Newline ('\n') rules * Definition/declaration of functions All keywords related with signature should be in same line. (extern/static, return type, parameters) When the signature is too long to represent in single line, try to break in parameters. * Space between function & etc. Use single newline to split paragraph in function code blocks. Use double newline to distinguish between function definitions. No need to use additional newline for declaring multiple functions. 3. Spacing rules * Control-flow statements Split control-flow statements, conditions, bracket with spaces. * Definition/declaration of functions Do not split function name and its parameter definition. * Calling functions Do not split argument passing parenthesis of function call. Signed-off-by: JaeSang Yoo --- hanjpautomata.c | 150 ++++++++++++++------------- hanjpbuffer.c | 65 +++++++----- hanjpbuffer.h | 6 +- hanjpinputcontext.c | 71 ++++++++----- hanjpinputcontext.h | 5 +- hanjpkeyboard.c | 39 ++++--- hanjpkeyboard.h | 8 +- hanjpunicode.h | 9 +- test/test_gobj_construct_dispose.cpp | 9 +- test/test_input_context_convert.cpp | 9 +- 10 files changed, 212 insertions(+), 159 deletions(-) diff --git a/hanjpautomata.c b/hanjpautomata.c index 16ecfb2..e49ddfd 100644 --- a/hanjpautomata.c +++ b/hanjpautomata.c @@ -8,15 +8,16 @@ extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); /* Kana Table index enums */ -enum { +enum +{ KANA_VOWEL_A, KANA_VOWEL_I, KANA_VOWEL_U, KANA_VOWEL_E, KANA_VOWEL_O }; - -enum { +enum +{ KANA_CONSONANT__, KANA_CONSONANT_K, KANA_CONSONANT_S, @@ -48,8 +49,10 @@ static const gunichar KANA_SMALL_TU = 0x3063; static const gunichar KANA_NN = 0x3093; // Combine multiple JungSeong into single int code -typedef union { - struct { +typedef union +{ + struct + { gunichar jung; gunichar jung2; }; @@ -62,12 +65,15 @@ typedef union { /* Automata Interface Definition */ G_DEFINE_INTERFACE(HanjpAutomata, hanjp_am, G_TYPE_OBJECT) -static void -hanjp_am_default_init(HanjpAutomataInterface *iface) { + +static void hanjp_am_default_init(HanjpAutomataInterface *iface) +{ /* add properties and signals to the interface here */ } -gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { + +gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -77,7 +83,9 @@ gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { return iface->to_kana(am, dest, buffer); } -gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar ch) { + +gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar ch) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -87,7 +95,9 @@ gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar c return iface->push(am, result, hangul, ch); } -gboolean hanjp_am_backspace(HanjpAutomata *am) { + +gboolean hanjp_am_backspace(HanjpAutomata *am) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -97,7 +107,9 @@ gboolean hanjp_am_backspace(HanjpAutomata *am) { return iface->backspace(am); } -void hanjp_am_flush(HanjpAutomata *am) { + +void hanjp_am_flush(HanjpAutomata *am) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -107,10 +119,10 @@ void hanjp_am_flush(HanjpAutomata *am) { iface->flush(am); } -/**/ /* AutomataBase Implementation */ -typedef struct { +typedef struct +{ HanjpBuffer buffer; GHashTable *combine_table; JungBox combine_table_keys[N_COMBINE_TABLE_ELEMENTS]; @@ -119,8 +131,9 @@ typedef struct { G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_am_base, G_TYPE_OBJECT) -static gint -choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) { + +static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) +{ gint ret = 0; // In normal indexing, it returns 0 *diacrit = 0; @@ -171,18 +184,18 @@ choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) { default: ret = -1; } + return ret; } -static void -divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) + +static void divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) { switch (buffer->jung) { case HANGUL_JUNGSEONG_WA: - if(*conso_idx == KANA_CONSONANT__) { + if (*conso_idx == KANA_CONSONANT__) { *conso_idx = KANA_CONSONANT_W; - } - else { + } else { buffer->jung = HANGUL_JUNGSEONG_O; buffer->jung2 = HANGUL_JUNGSEONG_A; } @@ -191,10 +204,9 @@ divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) case HANGUL_JUNGSEONG_YU: case HANGUL_JUNGSEONG_YO: case HANGUL_JUNGSEONG_YEO: - if(*conso_idx == KANA_CONSONANT__) { + if (*conso_idx == KANA_CONSONANT__) { *conso_idx = KANA_CONSONANT_Y; - } - else{ + } else { // Insert Jungseong_I before existing jungseong buffer->jung2 = buffer->jung; buffer->jung = HANGUL_JUNGSEONG_I; @@ -209,11 +221,11 @@ divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) } -static gint -jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { +static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) +{ gint ret = 0; // In normal indexing, it returns 0 - switch(jung) { + switch (jung) { case HANGUL_JUNGSEONG_WA: case HANGUL_JUNGSEONG_YA: case HANGUL_JUNGSEONG_A: @@ -244,8 +256,9 @@ jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { return ret; } -static gint -jongseong_to_kana(gunichar jong, gunichar *kana) { + +static gint jongseong_to_kana(gunichar jong, gunichar *kana) +{ gint ret = 0; // In normal conversion, it returns 0 switch (jong) { @@ -272,8 +285,8 @@ jongseong_to_kana(gunichar jong, gunichar *kana) { return ret; } -static gint -hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) + +static gint hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { gint i, r = 0; gint conso_idx, vowel_idx, diacrit; @@ -294,7 +307,7 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) hanjp_buffer_clear_filler(buffer); // check whether batchim is available and move choseong to jongseong if conditions are met - if(buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) { + if (buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) { kana = g_array_index(dest, gunichar, dest->len - 1); // Last kana character if(kana != KANA_NN && kana != KANA_SMALL_TU) { buffer->jong = hangul_choseong_to_jongseong(buffer->cho); @@ -303,8 +316,8 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) } //eat Choseong and Jungseong - while(buffer->cho || buffer->jung || buffer->jung2) { - // Convert choseong into kana indexing + while (buffer->cho || buffer->jung || buffer->jung2) { + // Convert choseong into kana indexing jamo = hanjp_buffer_pop_choseong(buffer); conv_err = choseong_to_kana_index(jamo, &conso_idx, &diacrit); if (conv_err && jamo == HANGUL_CHOSEONG_SSANGNIEUN) { @@ -323,7 +336,7 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) jungkey.jung = buffer->jung; jungkey.jung2 = buffer->jung2; comb_jungseong = (gunichar *)g_hash_table_lookup(priv->combine_table, &jungkey.value); - if(comb_jungseong != NULL) { + if (comb_jungseong != NULL) { buffer->jung = *comb_jungseong; buffer->jung2 = 0; } @@ -334,11 +347,11 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) //select column index jamo = hanjp_buffer_pop_jungseong(buffer); // victim - conv_err = jungseong_to_kana_index(jamo, &vowel_idx); + conv_err = jungseong_to_kana_index(jamo, &vowel_idx); if (conv_err) { r += hanjp_buffer_copy_jamoes(buffer, dest); return -r; - } + } // indexing done, convert into kana kana = KANA_TABLE[conso_idx][vowel_idx] + diacrit; @@ -354,8 +367,7 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) jamo = hangul_jongseong_to_choseong(jamo); hanjp_buffer_push(buffer, jamo); r += hanjp_am_base_to_kana(am, dest, buffer); - } - else if (jamo != 0) { + } else if (jamo != 0) { // When jamo were not empty, it may converted g_array_append_val(dest, kana); r++; @@ -364,8 +376,8 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) return r; } -static void -hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) + +static void hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) { JungBox jungkey; gunichar c; @@ -377,7 +389,7 @@ hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) hanjp_buffer_align_jungseong(&priv->buffer); - if(priv->buffer.jung != 0 && priv->buffer.jung2 != 0) { + if (priv->buffer.jung != 0 && priv->buffer.jung2 != 0) { jungkey.jung = priv->buffer.jung; jungkey.jung2 = priv->buffer.jung2; val = (gunichar*)g_hash_table_lookup(priv->combine_table, &jungkey.value); @@ -388,23 +400,22 @@ hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) } c = hangul_jamo_to_syllable(priv->buffer.cho, priv->buffer.jung, priv->buffer.jong); - if(c == 0) { - for(i = 0; i < 4; i++) { + if (c == 0) { + for (i = 0; i < 4; i++) { c = priv->buffer.stack[i]; - if(c != 0) { + if (c != 0) { c = hangul_jamo_to_cjamo(c); g_array_append_val(hangul, c); break; } } - } - else { + } else { g_array_append_val(hangul, c); } } -static gboolean -hanjp_am_base_backspace(HanjpAutomata *am) + +static gboolean hanjp_am_base_backspace(HanjpAutomata *am) { int i; HanjpAutomataBasePrivate *priv; @@ -413,16 +424,16 @@ hanjp_am_base_backspace(HanjpAutomata *am) return (hanjp_buffer_pop(&priv->buffer) != 0); } -static void -hanjp_am_base_flush(HanjpAutomata *am) + +static void hanjp_am_base_flush(HanjpAutomata *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); hanjp_buffer_flush(&priv->buffer); } -static void -hanjp_am_base_init(HanjpAutomataBase *am) + +static void hanjp_am_base_init(HanjpAutomataBase *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(am); @@ -431,8 +442,8 @@ hanjp_am_base_init(HanjpAutomataBase *am) priv->combine_table = g_hash_table_new(g_int64_hash, g_int64_equal); } -static void -hanjp_am_base_finalize(GObject *gobject) + +static void hanjp_am_base_finalize(GObject *gobject) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(gobject)); @@ -442,8 +453,8 @@ hanjp_am_base_finalize(GObject *gobject) G_OBJECT_CLASS(hanjp_am_base_parent_class)->finalize(gobject); } -static void -hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) + +static void hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -455,7 +466,6 @@ hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) klass->flush = hanjp_am_base_flush; } -/**/ /* AutomataBuiltin Implementation */ static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface); @@ -463,8 +473,8 @@ G_DEFINE_TYPE_WITH_CODE(HanjpAutomataBuiltin, hanjp_am_builtin, HANJP_TYPE_AM_BA G_IMPLEMENT_INTERFACE(HANJP_TYPE_AM, hanjp_am_builtin_interface_init)) -static void -hanjp_am_builtin_init(HanjpAutomataBuiltin *am) + +static void hanjp_am_builtin_init(HanjpAutomataBuiltin *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); @@ -475,8 +485,8 @@ hanjp_am_builtin_init(HanjpAutomataBuiltin *am) g_hash_table_insert(priv->combine_table, &priv->combine_table_keys[0].value, &priv->combine_table_vals[0]); } -static gint -hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunichar ch) + +static gint hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunichar ch) { gint r = 0; HanjpAutomataBasePrivate *priv; @@ -485,7 +495,7 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); buffer = &priv->buffer; - if(ch == 0) { + if (ch == 0) { return 0; } @@ -494,16 +504,15 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich // post-step // convert poped string to kana - if(ch != 0 && buffer->jong == 0) { + if (ch != 0 && buffer->jong == 0) { hanjp_am_base_peek(am, hangul); r = hanjp_am_base_to_kana(am, preedit, buffer); - if(!hangul_is_jamo(ch)) { + if (!hangul_is_jamo(ch)) { g_array_append_val(hangul, ch); g_array_append_val(preedit, ch); r++; r = -r; - } - else { + } else { hanjp_buffer_push(buffer, ch); } } @@ -512,21 +521,22 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich return r; } -static void -hanjp_am_builtin_class_init(HanjpAutomataBuiltinClass *klass) + +static void hanjp_am_builtin_class_init(HanjpAutomataBuiltinClass *klass) { HanjpAutomataBaseClass *base_class = HANJP_AM_BASE_CLASS(klass); base_class->push = hanjp_am_builtin_push; } + HanjpAutomataBuiltin *hanjp_am_builtin_new() { return g_object_new(HANJP_TYPE_AM_BUILTIN, NULL); } -static void -hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface) + +static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface) { iface->to_kana = hanjp_am_base_to_kana; iface->push = hanjp_am_builtin_push; diff --git a/hanjpbuffer.c b/hanjpbuffer.c index 1940042..638ea3a 100644 --- a/hanjpbuffer.c +++ b/hanjpbuffer.c @@ -9,24 +9,23 @@ extern ucschar hangul_jongseong_to_choseong(ucschar c); static const int BUFF_SIZE = (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); + /* HanjpBuffer Implementation */ -gunichar -hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { +gunichar hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) +{ if ((hangul_is_choseong(ch)) && (buffer->cho == 0)) { buffer->cho = ch; return 0; - } - else if (hangul_is_jungseong(ch)) { - if(buffer->jung == 0) { + } else if (hangul_is_jungseong(ch)) { + if (buffer->jung == 0) { buffer->jung = ch; return 0; } - else if(buffer->jung2 == 0) { + else if (buffer->jung2 == 0) { buffer->jung2 = ch; return 0; } - } - else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) { + } else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) { buffer->jong = ch; return 0; } @@ -34,8 +33,9 @@ hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { return ch; } -gint -hanjp_buffer_peek(HanjpBuffer *buffer) { + +gint hanjp_buffer_peek(HanjpBuffer *buffer) +{ int i; for (i = 0; i < BUFF_SIZE; i++) { @@ -47,8 +47,9 @@ hanjp_buffer_peek(HanjpBuffer *buffer) { return 0; } -gunichar -hanjp_buffer_pop(HanjpBuffer *buffer) { + +gunichar hanjp_buffer_pop(HanjpBuffer *buffer) +{ gint ret = 0; int i; @@ -63,16 +64,18 @@ hanjp_buffer_pop(HanjpBuffer *buffer) { return ret; } -void -hanjp_buffer_flush(HanjpBuffer *buffer) { + +void hanjp_buffer_flush(HanjpBuffer *buffer) +{ buffer->cho = 0; buffer->jung = 0; buffer->jung2 = 0; buffer->jong = 0; } -bool -hanjp_buffer_is_valid(HanjpBuffer *buffer) { + +bool hanjp_buffer_is_valid(HanjpBuffer *buffer) +{ int i; for (i = 0; i < BUFF_SIZE; i++) { @@ -84,8 +87,9 @@ hanjp_buffer_is_valid(HanjpBuffer *buffer) { return true; } -gint -hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) { + +gint hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) +{ int i; int copy_cnt = 0; @@ -98,8 +102,9 @@ hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) { return copy_cnt; } -void -hanjp_buffer_clear_filler(HanjpBuffer *buffer) { + +void hanjp_buffer_clear_filler(HanjpBuffer *buffer) +{ int i; for (i = 0; i < BUFF_SIZE; i++) { @@ -110,32 +115,36 @@ hanjp_buffer_clear_filler(HanjpBuffer *buffer) { } } -void -hanjp_buffer_align_jungseong(HanjpBuffer *buffer) { + +void hanjp_buffer_align_jungseong(HanjpBuffer *buffer) +{ if (buffer->jung == 0) { buffer->jung = buffer-> jung2; buffer->jung2 = 0; } } -gunichar -hanjp_buffer_pop_choseong(HanjpBuffer *buffer) { + +gunichar hanjp_buffer_pop_choseong(HanjpBuffer *buffer) +{ gunichar ch = buffer->cho; buffer->cho = 0; return ch; } -gunichar -hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) { + +gunichar hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) +{ gunichar ch = buffer->jung; buffer->jung = 0; return ch; } -gunichar -hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) { + +gunichar hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) +{ gunichar ch = buffer->jong; buffer->jong = 0; diff --git a/hanjpbuffer.h b/hanjpbuffer.h index 2f3a3c0..97fc7b6 100644 --- a/hanjpbuffer.h +++ b/hanjpbuffer.h @@ -5,8 +5,10 @@ #include #include -typedef union { - struct { +typedef union +{ + struct + { gunichar cho; gunichar jung; gunichar jung2; diff --git a/hanjpinputcontext.c b/hanjpinputcontext.c index a6a5b67..bb69e5d 100644 --- a/hanjpinputcontext.c +++ b/hanjpinputcontext.c @@ -2,17 +2,22 @@ #include "hanjpautomata.h" #include "hanjpkeyboard.h" -static gboolean is_hiragana(gunichar ch) { +static gboolean is_hiragana(gunichar ch) +{ return (ch >= 0x3041 && ch <= 0x3096) || (ch >= 0x309D && ch <= 0x309E); } -static gboolean is_katakana(gunichar ch) { + +static gboolean is_katakana(gunichar ch) +{ return (ch >= 0x30A1 && ch <= 0x30F6) || (ch >= 0x30FD && ch <= 0x30FE); } + #define KANA_GAP 0x60 -typedef struct { +typedef struct +{ HanjpAutomata *ams[2]; HanjpAutomata *cur_am; HanjpKeyboard *keyboard; @@ -25,8 +30,8 @@ typedef struct { G_DEFINE_TYPE_WITH_PRIVATE(HanjpInputContext, hanjp_ic, G_TYPE_OBJECT) -static void -hanjp_ic_dispose(GObject *self) + +static void hanjp_ic_dispose(GObject *self) { HanjpInputContextPrivate *priv; priv = hanjp_ic_get_instance_private(HANJP_INPUTCONTEXT(self)); @@ -46,14 +51,14 @@ hanjp_ic_dispose(GObject *self) G_OBJECT_CLASS(hanjp_ic_parent_class)->dispose(self); } -static void -hanjp_ic_finalize(GObject *self) + +static void hanjp_ic_finalize(GObject *self) { G_OBJECT_CLASS(hanjp_ic_parent_class)->finalize(self); } -static void -hanjp_ic_class_init(HanjpInputContextClass *klass) + +static void hanjp_ic_class_init(HanjpInputContextClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -61,8 +66,8 @@ hanjp_ic_class_init(HanjpInputContextClass *klass) object_class->finalize = hanjp_ic_finalize; } -static void -hanjp_ic_init(HanjpInputContext *self) + +static void hanjp_ic_init(HanjpInputContext *self) { gint i; HanjpInputContextPrivate *priv; @@ -79,11 +84,13 @@ hanjp_ic_init(HanjpInputContext *self) priv->output_mode = HANJP_OUTPUT_HIRAGANA; } + HanjpInputContext* hanjp_ic_new() { return g_object_new(HANJP_TYPE_INPUTCONTEXT, NULL); } + void hanjp_ic_reset(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -99,6 +106,7 @@ void hanjp_ic_reset(HanjpInputContext *self) priv->output_mode = HANJP_OUTPUT_HIRAGANA; } + void hanjp_ic_flush(HanjpInputContext *self) { gint i; @@ -108,13 +116,14 @@ void hanjp_ic_flush(HanjpInputContext *self) priv = hanjp_ic_get_instance_private(self); hanjp_am_flush(priv->cur_am); - for(i = 0; i < priv->preedit->len; i++) { + for (i = 0; i < priv->preedit->len; i++) { g_array_append_val(priv->committed, g_array_index(priv->preedit, gunichar, i)); } g_array_set_size(priv->preedit, 0); priv->kana_len = 0; } + gint hanjp_ic_process(HanjpInputContext *self, gint ascii) { int i; @@ -127,7 +136,7 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) //map jaso from ascii ch = hanjp_kb_get_mapping(priv->keyboard, 0, ascii); - if(ch == 0) { + if (ch == 0) { ch = (gunichar)ascii; } //shrink preedit before push @@ -137,15 +146,14 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) //push jaso into automata res = hanjp_am_push(priv->cur_am, priv->preedit, priv->hangul, ch); - if(res < 0) { + if (res < 0) { priv->kana_len += -res; hanjp_ic_flush(self); - } - else if(res > 0) { + } else if(res > 0) { // if mode is katakana, change result to katakana - if(priv->output_mode == HANJP_OUTPUT_KATAKANA) { - for(i = priv->kana_len; i < priv->preedit->len; i++) { - if(!is_hiragana(g_array_index(priv->preedit, gunichar, i))){ + if (priv->output_mode == HANJP_OUTPUT_KATAKANA) { + for (i = priv->kana_len; i < priv->preedit->len; i++) { + if (!is_hiragana(g_array_index(priv->preedit, gunichar, i))) { continue; } g_array_index(priv->preedit, gunichar, i) += KANA_GAP; @@ -157,6 +165,7 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) return res; } + void hanjp_ic_toggle_preedit(HanjpInputContext *self) { int i; @@ -174,6 +183,7 @@ void hanjp_ic_toggle_preedit(HanjpInputContext *self) } } + void hanjp_ic_to_haragana_preedit(HanjpInputContext *self) { int i; @@ -189,7 +199,9 @@ void hanjp_ic_to_haragana_preedit(HanjpInputContext *self) } } -void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) { + +void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) +{ int i; HanjpInputContextPrivate *priv; @@ -203,6 +215,7 @@ void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) { } } + void hanjp_ic_replace(HanjpInputContext *self, int start, int end, const gunichar* str_insert) { int i; @@ -215,14 +228,14 @@ void hanjp_ic_replace(HanjpInputContext *self, int start, int end, const gunicha g_array_remove_range(priv->preedit, start, end - start); - while (*str_insert) - { + while (*str_insert) { g_array_insert_val(priv->preedit, start, *str_insert); start++; str_insert++; } } + gboolean hanjp_ic_backspace(HanjpInputContext *self) { g_return_if_fail(HANJP_IS_INPUTCONTEXT(self)); @@ -230,21 +243,18 @@ gboolean hanjp_ic_backspace(HanjpInputContext *self) HanjpInputContextPrivate *priv; priv = hanjp_ic_get_instance_private(self); - if(!hanjp_am_backspace(priv->cur_am)){ - - if((priv->preedit->len)==0) - { + if (!hanjp_am_backspace(priv->cur_am)) { + if ((priv->preedit->len)==0) { return FALSE; } g_array_remove_index(priv->preedit,(priv->preedit->len)-1); - } return TRUE; - } + GArray* hanjp_ic_ref_preedit_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -255,6 +265,7 @@ GArray* hanjp_ic_ref_preedit_string(HanjpInputContext *self) return g_array_ref(priv->preedit); } + GArray* hanjp_ic_ref_commit_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -265,6 +276,7 @@ GArray* hanjp_ic_ref_commit_string(HanjpInputContext *self) return g_array_ref(priv->committed); } + GArray* hanjp_ic_ref_hangul_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -275,6 +287,7 @@ GArray* hanjp_ic_ref_hangul_string(HanjpInputContext *self) return g_array_ref(priv->hangul); } + void hanjp_ic_set_am(HanjpInputContext *self, gint i) { HanjpInputContextPrivate *priv; @@ -286,6 +299,7 @@ void hanjp_ic_set_am(HanjpInputContext *self, gint i) priv->cur_am = g_object_ref(priv->ams[i]); } + void hanjp_ic_set_output_mode(HanjpInputContext *self, gint i) { HanjpInputContextPrivate *priv; @@ -295,6 +309,7 @@ void hanjp_ic_set_output_mode(HanjpInputContext *self, gint i) priv->output_mode = i; } + gint hanjp_ic_get_output_mode(HanjpInputContext *self) { HanjpInputContextPrivate *priv; diff --git a/hanjpinputcontext.h b/hanjpinputcontext.h index 6d99713..0af44ad 100644 --- a/hanjpinputcontext.h +++ b/hanjpinputcontext.h @@ -6,7 +6,8 @@ G_BEGIN_DECLS -enum { +enum +{ HANJP_OUTPUT_HIRAGANA, HANJP_OUTPUT_KATAKANA, HANJP_OUTPUT_HALFKATAKANA @@ -53,4 +54,4 @@ gint hanjp_ic_get_output_mode(HanjpInputContext *self); G_END_DECLS -#endif // __HANJP_INPUTCONTEXT_H__ \ No newline at end of file +#endif // __HANJP_INPUTCONTEXT_H__ diff --git a/hanjpkeyboard.c b/hanjpkeyboard.c index 3bba2a7..ac946c1 100644 --- a/hanjpkeyboard.c +++ b/hanjpkeyboard.c @@ -1,16 +1,17 @@ #include "hanjpkeyboard.h" #include "hangul.h" -extern ucschar -hangul_keyboard_get_mapping(const HangulKeyboard* keyboard, int tableid, unsigned key); +extern ucschar hangul_keyboard_get_mapping(const HangulKeyboard* keyboard, int tableid, unsigned key); G_DEFINE_INTERFACE(HanjpKeyboard, hanjp_kb, G_TYPE_OBJECT) -static void -hanjp_kb_default_init(HanjpKeyboardInterface *iface) { + +static void hanjp_kb_default_init(HanjpKeyboardInterface *iface) +{ // Nothing to do } + gunichar hanjp_kb_get_mapping(HanjpKeyboard* self, gint tableid, gint ascii) { HanjpKeyboardInterface *iface; @@ -22,23 +23,27 @@ gunichar hanjp_kb_get_mapping(HanjpKeyboard* self, gint tableid, gint ascii) return iface->get_mapping(self, tableid, ascii); } -typedef struct { + +typedef struct +{ HangulKeyboard *keyboard; } HanjpKeyboardBuiltinPrivate; + static void hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface); G_DEFINE_TYPE_WITH_CODE(HanjpKeyboardBuiltin, hanjp_kb_builtin, G_TYPE_OBJECT, G_ADD_PRIVATE(HanjpKeyboardBuiltin) G_IMPLEMENT_INTERFACE(HANJP_TYPE_KB, hanjp_kb_builtin_interface_init)) + HanjpKeyboardBuiltin *hanjp_kb_builtin_new() { return g_object_new(HANJP_TYPE_KEYBOARDDEFAULT, NULL); } -static gunichar -hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) + +static gunichar hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(HANJP_KB_BUILTIN(self)); @@ -47,8 +52,8 @@ hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) return hangul_keyboard_get_mapping(priv->keyboard, tableid, ascii); } -static void -hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) + +static void hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(self); @@ -56,14 +61,14 @@ hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) priv->keyboard = hangul_keyboard_list_get_keyboard("2"); } -static void -hanjp_kb_builtin_dispose(GObject *gobject) + +static void hanjp_kb_builtin_dispose(GObject *gobject) { G_OBJECT_CLASS(hanjp_kb_builtin_parent_class)->dispose(gobject); } -static void -hanjp_kb_builtin_finalize(GObject *gobject) + +static void hanjp_kb_builtin_finalize(GObject *gobject) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(HANJP_KB_BUILTIN(gobject)); @@ -71,8 +76,8 @@ hanjp_kb_builtin_finalize(GObject *gobject) G_OBJECT_CLASS(hanjp_kb_builtin_parent_class)->finalize(gobject); } -static void -hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) + +static void hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -80,8 +85,8 @@ hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) object_class->finalize = hanjp_kb_builtin_finalize; } -static void -hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface) + +static void hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface) { iface->get_mapping = hanjp_kb_builtin_get_mapping; } diff --git a/hanjpkeyboard.h b/hanjpkeyboard.h index f94f699..f820028 100644 --- a/hanjpkeyboard.h +++ b/hanjpkeyboard.h @@ -8,7 +8,8 @@ G_BEGIN_DECLS #define HANJP_TYPE_KB hanjp_kb_get_type() G_DECLARE_INTERFACE(HanjpKeyboard, hanjp_kb, HANJP, KB, GObject) -struct _HanjpKeyboardInterface { +struct _HanjpKeyboardInterface +{ GTypeInterface parent_iface; gunichar (*get_mapping) (HanjpKeyboard *self, gint tableid, gint ascii); @@ -19,7 +20,8 @@ gunichar hanjp_kb_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii); #define HANJP_TYPE_KEYBOARDDEFAULT hanjp_kb_builtin_get_type() G_DECLARE_DERIVABLE_TYPE(HanjpKeyboardBuiltin, hanjp_kb_builtin, HANJP, KB_BUILTIN, GObject) -struct _HanjpKeyboardBuiltinClass { +struct _HanjpKeyboardBuiltinClass +{ GObjectClass parent_class; }; @@ -27,4 +29,4 @@ HanjpKeyboardBuiltin *hanjp_kb_builtin_new(); G_END_DECLS -#endif // __HANJP_KEYBOARD_H__ \ No newline at end of file +#endif // __HANJP_KEYBOARD_H__ diff --git a/hanjpunicode.h b/hanjpunicode.h index 7801b48..d8384ff 100644 --- a/hanjpunicode.h +++ b/hanjpunicode.h @@ -4,7 +4,8 @@ // The belows are Unicode presets for Hangul syllables // syllable leading consonants Jamo (초성) -enum { +enum +{ // modern Hangul leading consonants HANGUL_CHOSEONG_KIYEOK = 0x1100, HANGUL_CHOSEONG_SSANGKIYEOK, @@ -105,7 +106,8 @@ enum { }; // syllable medial vowels Jamo (중성) -enum { +enum +{ // modern Hangul medial vowels HANGUL_JUNGSEONG_A = 0x1161, HANGUL_JUNGSEONG_AE, @@ -182,7 +184,8 @@ enum { }; // syllable trailing consonants jamo (초성) -enum { +enum +{ // modern Hangul trailing consonants HANGUL_JONGSEONG_KIYEOK = 0x11A8, // ㄱ HANGUL_JONGSEONG_SSANGKIYEOK, // ㄲ diff --git a/test/test_gobj_construct_dispose.cpp b/test/test_gobj_construct_dispose.cpp index 61b9244..803711f 100644 --- a/test/test_gobj_construct_dispose.cpp +++ b/test/test_gobj_construct_dispose.cpp @@ -4,7 +4,8 @@ #include "hanjpinputcontext.h" -TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) { +TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) +{ // Given HanjpAutomataDefault object HanjpAutomataBuiltin *amBuiltin = NULL; const char* strExpectObjType = "HanjpAutomataBuiltin"; @@ -30,7 +31,8 @@ TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) { } -TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) { +TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) +{ // Given HanjpKeyboardDefault object HanjpKeyboardBuiltin *kbBuiltin = NULL; const char* strExpectObjType = "HanjpKeyboardBuiltin"; @@ -56,7 +58,8 @@ TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) { } -TEST(GObjectConstructDisposeTest, InputContextConstructDispose) { +TEST(GObjectConstructDisposeTest, InputContextConstructDispose) +{ // Given HanjpInputContext object HanjpInputContext *ic = NULL; const char* strExpectObjType = "HanjpInputContext"; diff --git a/test/test_input_context_convert.cpp b/test/test_input_context_convert.cpp index bf7ba9f..ae3c057 100644 --- a/test/test_input_context_convert.cpp +++ b/test/test_input_context_convert.cpp @@ -1,7 +1,8 @@ #include #include "hanjpinputcontext.h" -class InputContextTest : public ::testing::Test { +class InputContextTest : public ::testing::Test +{ protected: void SetUp() override { // Create context object for each tests @@ -16,7 +17,8 @@ class InputContextTest : public ::testing::Test { HanjpInputContext *context; }; -TEST_F(InputContextTest, IC_Hajimemashite) { +TEST_F(InputContextTest, IC_Hajimemashite) +{ const char *strGivenKeyInput = "gkwlapaktlxp."; const char *strExpectJapanese = "はじめまして."; const char *strActualJapanese = NULL; @@ -38,7 +40,8 @@ TEST_F(InputContextTest, IC_Hajimemashite) { g_array_unref(committed); } -TEST_F(InputContextTest, IC_KYOUWAKOKU) { +TEST_F(InputContextTest, IC_KYOUWAKOKU) +{ const char *strGivenKeyInput = "zydndhkzhzn."; const char *strExpectJapenese = "きょうわこく."; const char *strActualJapanese = NULL; From 219f3e22fd161a7079d10a208bdd1d0bb51f4d69 Mon Sep 17 00:00:00 2001 From: JaeSang Yoo Date: Fri, 8 Apr 2022 11:59:11 +0900 Subject: [PATCH 3/5] refactor: improve readability in kana conversion * declare conversion functions near its related constants definition * change magic numbers (conversion result) into constants Signed-off-by: JaeSang Yoo --- hanjpautomata.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/hanjpautomata.c b/hanjpautomata.c index e49ddfd..0332dab 100644 --- a/hanjpautomata.c +++ b/hanjpautomata.c @@ -48,6 +48,15 @@ static const gunichar KANA_TABLE[][5] = { static const gunichar KANA_SMALL_TU = 0x3063; static const gunichar KANA_NN = 0x3093; +/* Jamo to Kana conversion */ +#define KANA_CONV_SUCCESS (0) +#define KANA_CONV_FAIL (-1) + +static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit); +static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx); +static gint jongseong_to_kana(gunichar jong, gunichar *kana); + + // Combine multiple JungSeong into single int code typedef union { @@ -134,10 +143,7 @@ G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_am_base, G_TYPE_OBJECT) static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) { - gint ret = 0; // In normal indexing, it returns 0 - *diacrit = 0; - // Select row index and set adjuster switch (cho) { case 0: // VOID *diacrit = -1; @@ -182,10 +188,10 @@ static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) break; case HANGUL_CHOSEONG_SSANGNIEUN: default: - ret = -1; + return KANA_CONV_FAIL; } - return ret; + return KANA_CONV_SUCCESS; } @@ -223,8 +229,6 @@ static void divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { - gint ret = 0; // In normal indexing, it returns 0 - switch (jung) { case HANGUL_JUNGSEONG_WA: case HANGUL_JUNGSEONG_YA: @@ -251,16 +255,15 @@ static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) *vowel_idx = KANA_VOWEL_O; break; default: - ret = -1; + return KANA_CONV_FAIL; } - return ret; + + return KANA_CONV_SUCCESS; } static gint jongseong_to_kana(gunichar jong, gunichar *kana) { - gint ret = 0; // In normal conversion, it returns 0 - switch (jong) { case HANGUL_JONGSEONG_KIYEOK: case HANGUL_JONGSEONG_SSANGKIYEOK: @@ -280,9 +283,10 @@ static gint jongseong_to_kana(gunichar jong, gunichar *kana) *kana = 0; break; default: - ret = -1; + return KANA_CONV_FAIL; } - return ret; + + return KANA_CONV_SUCCESS; } From 5c22882757eedcc0f05d47882503b80c34a1dda4 Mon Sep 17 00:00:00 2001 From: JaeSang Yoo Date: Fri, 15 Apr 2022 10:48:46 +0900 Subject: [PATCH 4/5] chore: format comment styles * Prefer use single line comment with // * To describe simple comment about codeblock below * To describe after the code line * Prefer use multiple line comment with /* */ * Always use /* */ for multiple line comment * To emphasize code block below (ex. Interface Implementation) * Documentation such as doxygen (not applied yet) Signed-off-by: JaeSang Yoo --- hanjpautomata.c | 74 +++++++++++++++++++++++++-------------------- hanjpbuffer.c | 6 ++-- hanjpinputcontext.c | 8 ++--- hanjpkeyboard.c | 5 +++ hanjpunicode.h | 2 +- 5 files changed, 56 insertions(+), 39 deletions(-) diff --git a/hanjpautomata.c b/hanjpautomata.c index 0332dab..53c6986 100644 --- a/hanjpautomata.c +++ b/hanjpautomata.c @@ -3,22 +3,25 @@ #include #include -/* Extern function signature which isn't exported in libhangul header */ +// Extern function signature which aren't exported in libhangul header extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); -/* Kana Table index enums */ + +/* + * Kana Table + */ +// table indexing enums enum { - KANA_VOWEL_A, + // Vowel indices + KANA_VOWEL_A = 0, KANA_VOWEL_I, KANA_VOWEL_U, KANA_VOWEL_E, - KANA_VOWEL_O -}; -enum -{ - KANA_CONSONANT__, + KANA_VOWEL_O, + // Consonant indices + KANA_CONSONANT__ = 0, KANA_CONSONANT_K, KANA_CONSONANT_S, KANA_CONSONANT_T, @@ -30,28 +33,30 @@ enum KANA_CONSONANT_W }; -// Fifty notes -// For example か(ka) = KANA_TABLE[KANA_CONSONANT_K][KANA_VOWEL_A] +// Fifty notes, ex. か(ka) = KANA_TABLE[KANA_CONSONANT_K][KANA_VOWEL_A] static const gunichar KANA_TABLE[][5] = { - // A, I, U, E, O - {0x3042, 0x3044, 0x3046, 0x3048, 0x304A}, // A - {0x304B, 0x304D, 0x304F, 0x3051, 0x3053}, // KA - {0x3055, 0x3057, 0x3059, 0x305B, 0x305D}, // SA - {0x305F, 0x3061, 0x3064, 0x3066, 0x3068}, // TA - {0x306A, 0x306B, 0x306C, 0x306D, 0x306E}, // NA - {0x306F, 0x3072, 0x3075, 0x3078, 0x307B}, // HA - {0x307E, 0x307F, 0x3080, 0x3081, 0x3082}, // MO - {0x3084, 0x0000, 0x3086, 0x0000, 0x3088}, // YA - {0x3089, 0x308A, 0x308B, 0x308C, 0x308D}, // RA - {0x308F, 0x3090, 0x0000, 0x3091, 0x3092} // WA + // _A, _I, _U, _E, _O + { 0x3042, 0x3044, 0x3046, 0x3048, 0x304A }, // _ + { 0x304B, 0x304D, 0x304F, 0x3051, 0x3053 }, // K_ + { 0x3055, 0x3057, 0x3059, 0x305B, 0x305D }, // S_ + { 0x305F, 0x3061, 0x3064, 0x3066, 0x3068 }, // T_ + { 0x306A, 0x306B, 0x306C, 0x306D, 0x306E }, // N_ + { 0x306F, 0x3072, 0x3075, 0x3078, 0x307B }, // H_ + { 0x307E, 0x307F, 0x3080, 0x3081, 0x3082 }, // M_ + { 0x3084, 0x0000, 0x3086, 0x0000, 0x3088 }, // Y_ + { 0x3089, 0x308A, 0x308B, 0x308C, 0x308D }, // R_ + { 0x308F, 0x3090, 0x0000, 0x3091, 0x3092 } // W_ }; + +// Other Kana characters (which cannot be indexed through KANA_TABLE static const gunichar KANA_SMALL_TU = 0x3063; static const gunichar KANA_NN = 0x3093; -/* Jamo to Kana conversion */ +// Jamo to Kana conversion results #define KANA_CONV_SUCCESS (0) #define KANA_CONV_FAIL (-1) +// Jamo to Kana conversion functions static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit); static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx); static gint jongseong_to_kana(gunichar jong, gunichar *kana); @@ -71,13 +76,15 @@ typedef union #define N_COMBINE_TABLE_ELEMENTS 30 -/* Automata Interface Definition */ +/* + * Automata Interface Definition + */ G_DEFINE_INTERFACE(HanjpAutomata, hanjp_am, G_TYPE_OBJECT) static void hanjp_am_default_init(HanjpAutomataInterface *iface) { - /* add properties and signals to the interface here */ + // Nothing to do } @@ -129,7 +136,9 @@ void hanjp_am_flush(HanjpAutomata *am) } -/* AutomataBase Implementation */ +/* + * AutomataBase Implementation + */ typedef struct { HanjpBuffer buffer; @@ -138,6 +147,7 @@ typedef struct guint32 combine_table_vals[N_COMBINE_TABLE_ELEMENTS]; } HanjpAutomataBasePrivate; + G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_am_base, G_TYPE_OBJECT) @@ -226,7 +236,7 @@ static void divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) } } - + static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { switch (jung) { @@ -310,7 +320,7 @@ static gint hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer * hanjp_buffer_clear_filler(buffer); - // check whether batchim is available and move choseong to jongseong if conditions are met + // When Batchim is available, then move choseong to jongseong if (buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) { kana = g_array_index(dest, gunichar, dest->len - 1); // Last kana character if(kana != KANA_NN && kana != KANA_SMALL_TU) { @@ -319,7 +329,7 @@ static gint hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer * } } - //eat Choseong and Jungseong + // eat Choseong and Jungseong while (buffer->cho || buffer->jung || buffer->jung2) { // Convert choseong into kana indexing jamo = hanjp_buffer_pop_choseong(buffer); @@ -471,11 +481,12 @@ static void hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) } -/* AutomataBuiltin Implementation */ +/* + * AutomataBuiltin Implementation + */ static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface); G_DEFINE_TYPE_WITH_CODE(HanjpAutomataBuiltin, hanjp_am_builtin, HANJP_TYPE_AM_BASE, - G_IMPLEMENT_INTERFACE(HANJP_TYPE_AM, - hanjp_am_builtin_interface_init)) + G_IMPLEMENT_INTERFACE(HANJP_TYPE_AM, hanjp_am_builtin_interface_init)) static void hanjp_am_builtin_init(HanjpAutomataBuiltin *am) @@ -547,5 +558,4 @@ static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface) iface->backspace = hanjp_am_base_backspace; iface->flush = hanjp_am_base_flush; } - /**/ diff --git a/hanjpbuffer.c b/hanjpbuffer.c index 638ea3a..52e1d41 100644 --- a/hanjpbuffer.c +++ b/hanjpbuffer.c @@ -2,7 +2,7 @@ #include #include -/* Extern function signature which isn't exported in libhangul header */ +// Extern function signature which aren't exported in libhangul header extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); @@ -10,7 +10,9 @@ static const int BUFF_SIZE = (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); -/* HanjpBuffer Implementation */ +/* + * HanjpBuffer Implementation + */ gunichar hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { if ((hangul_is_choseong(ch)) && (buffer->cho == 0)) { diff --git a/hanjpinputcontext.c b/hanjpinputcontext.c index bb69e5d..7aa788a 100644 --- a/hanjpinputcontext.c +++ b/hanjpinputcontext.c @@ -47,7 +47,7 @@ static void hanjp_ic_dispose(GObject *self) g_array_unref(priv->hangul); priv->hangul = NULL; - //chain up + // chain up G_OBJECT_CLASS(hanjp_ic_parent_class)->dispose(self); } @@ -134,16 +134,16 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) g_return_if_fail(HANJP_IS_INPUTCONTEXT(self)); priv = hanjp_ic_get_instance_private(self); - //map jaso from ascii + // map jaso from ascii ch = hanjp_kb_get_mapping(priv->keyboard, 0, ascii); if (ch == 0) { ch = (gunichar)ascii; } - //shrink preedit before push + // shrink preedit before push g_array_set_size(priv->preedit, priv->kana_len); g_array_set_size(priv->committed, 0); g_array_set_size(priv->hangul, 0); - //push jaso into automata + // push jaso into automata res = hanjp_am_push(priv->cur_am, priv->preedit, priv->hangul, ch); if (res < 0) { diff --git a/hanjpkeyboard.c b/hanjpkeyboard.c index ac946c1..431f09a 100644 --- a/hanjpkeyboard.c +++ b/hanjpkeyboard.c @@ -1,8 +1,13 @@ #include "hanjpkeyboard.h" #include "hangul.h" +// Extern function signature which isn't exported in libhangul header extern ucschar hangul_keyboard_get_mapping(const HangulKeyboard* keyboard, int tableid, unsigned key); + +/* + * Keyboard Interface Definition + */ G_DEFINE_INTERFACE(HanjpKeyboard, hanjp_kb, G_TYPE_OBJECT) diff --git a/hanjpunicode.h b/hanjpunicode.h index d8384ff..b9ef7f8 100644 --- a/hanjpunicode.h +++ b/hanjpunicode.h @@ -183,7 +183,7 @@ enum HANGUL_JUNGSEONG_O_YAE }; -// syllable trailing consonants jamo (초성) +// syllable trailing consonants jamo (종성) enum { // modern Hangul trailing consonants From 4ae1c7efce80e7df4ff8129ae7c8ac1297b1c7ff Mon Sep 17 00:00:00 2001 From: JaeSang Yoo Date: Fri, 15 Apr 2022 12:12:13 +0900 Subject: [PATCH 5/5] docs: Add coding convention document * Defines coding convention * Rules are proposed in commit bb79de2, 9b76bfe, 5c22882 * General rules are refereneced from zeromq Signed-off-by: JaeSang Yoo --- CODE_CONVENTION.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 CODE_CONVENTION.md diff --git a/CODE_CONVENTION.md b/CODE_CONVENTION.md new file mode 100644 index 0000000..b383c73 --- /dev/null +++ b/CODE_CONVENTION.md @@ -0,0 +1,60 @@ +# Coding Conventions + +For better readability of codes, there are coding conventions to follow. +Other dependent codes (library such as libhangul) are not applied by this convention. + +## Indent + +* Use 4 spaces for indentation. +* Tabs aren't allowed whether it generates compile errors. + +## Identifiers + +* Use `snake_case` for functions, variables names. +* Use `CONSTANT_CASE` for constant variables, constant macros, enums. +* Use `PascalCase` for defining struct(class) type names. +* Other casing rules can be used in their circumstances, but prefer rules above. + * GObject, GLib may use PascalCase for struct(class) type names. + * GoogleTest prefers not to use underscore(`_`) in function/test namings. + +## Spacing + +### Horizontal spacing + +* Split control-flow statements, conditions, bracket with spaces. +* Do not split function name and its parameter definition with spaces. + +### Vertical spacing (line breaks) + +* Use single line break to split paragraph in function code blocks. +* Use double line break between function definitions for better distinguish. +* Declaring multiple functions and variables can skip line break by its context. +* Try to write all keywords on defining/declaring function and its signature. + * Try to write `extern`/`static`, `inline`, return type and function name in same line. + * When the signature is too long to represent in single line, try to break in parameters. +* For unary operators such as `&`, `++`, `*`(pointer dereference), do not separate. +* For binary operators such as `+`, `-`, `==`, separate with spaces. + +### Bracket `{`, `}` + +* For function, structure, union, class and enums: + opening and closing brackets must be placed on separate line. +* For initializing struct, array: + opening and closing brackets must be placed on separate line. + When the initialization is short enough to place in single line, it's allowed. +* For control-flow statements (ex. `if`, `for`, `while`...): + opening bracket must be placed on same line. + on following `else`, `else if` statements, closing bracket must be placed on same line. + (ex. `} else {`) +* When the control-flow handles single line expression, bracket can be skipped. + But, one of `if`, `else if` and `else` has multiple line expression, + use bracket even the others handle single line expression. + +## Comments + +* Both C style (`/* ... */`) and C++ style (`// ...`) comments are allowed. +* For consistency, try to use each styles for: + * Use C style for multiple line comments. Such as documentation, long descriptions. + * Use C++ style for single line comments. Such as supplimentary, short descriptions. +* Use space after/before comment specifiers. +