Skip to content

Commit

Permalink
Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEWOOCHAN committed Jun 28, 2020
1 parent b58633f commit 2a5c057
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
11 changes: 2 additions & 9 deletions app/src/test/java/com/chani/hangulbuilder/HangulBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,23 @@ class HangulBuilderTest {
val builder = HangulBuilder

@Test
fun assembleTest() {
fun composeTest() {
builder.compose('', '').also { assertThat(it).isEqualTo("") }

builder.compose('', '', '').also { assertThat(it).isEqualTo("") }
}

@Test
fun disassembleTest() {
fun decomposeTest() {
builder.decompose("가나다라").also { assertThat(it).isEqualTo("ㄹㅏ") }

builder.decompose("").also { assertThat(it).isEqualTo("ㄱㅏ") }

builder.decompose("가나다라", isSeparateAll = true).also {
assertThat(it).isEqualTo("ㄱㅏㄴㅏㄷㅏㄹㅏ")
}

builder.decompose("가나다라", ",", true).also {
assertThat(it).isEqualTo("ㄱ,ㅏ,ㄴ,ㅏ,ㄷ,ㅏ,ㄹ,ㅏ")
}

builder.decompose(" ").also { assertThat(it).isEqualTo(" ") }

builder.decompose("가나다ABC").also { assertThat(it).isEqualTo("C") }

builder.decompose("가나다ABC나다라").also { assertThat(it).isEqualTo("ㄹㅏ") }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ package com.chani.hangulbuilder

import java.lang.StringBuilder

/**
* Copyright (C) 2020 dncks1525
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

object HangulBuilder {
private const val BASE_UNICODE = ''
private const val JUNGSEONG_COUNT = 21
Expand Down Expand Up @@ -45,7 +61,7 @@ object HangulBuilder {
Detachable('', '', '')
)

private val strBuilder = StringBuilder()
private val decomposeBuilder = StringBuilder()
private val hangulBuilder = StringBuilder()

fun add(vararg str: String): String {
Expand Down Expand Up @@ -81,10 +97,11 @@ object HangulBuilder {
}
}
last.isHangulLetter() -> {
hangulBuilder.delete(size - 2, size)
decompose(last.toString()).apply {
if (this.length > 2) {
if (target.isVowel()) {
hangulBuilder.delete(size - 2, size)

val consonants =
detachableConsonants.find { it.origin == this[2] }
if (consonants != null) {
Expand All @@ -101,6 +118,7 @@ object HangulBuilder {
detachableConsonants.find {
it.first == this[2] && it.second == target
}?.let {
hangulBuilder.delete(size - 2, size)
hangulBuilder.append(compose(this[0], this[1], it.origin))
}
}
Expand All @@ -111,12 +129,14 @@ object HangulBuilder {
detachableVowels.find {
it.first == this[1] && it.second == target
}?.let {
hangulBuilder.delete(size - 2, size)
hangulBuilder.append(compose(this[0], it.origin))
}
}
target.isConsonant() -> {
// ex) 고 + ㄱ -> 곡
if (jongseongList.contains(target)) {
hangulBuilder.delete(size - 2, size)
hangulBuilder.append(compose(this[0], this[1], target))
}
}
Expand Down Expand Up @@ -170,6 +190,9 @@ object HangulBuilder {
hangulBuilder.append(it.first)
}
}
else -> {
hangulBuilder.delete(size-1, size)
}
}
}

Expand All @@ -191,18 +214,18 @@ object HangulBuilder {
fun decompose(str: String, separator: String = "", isSeparateAll: Boolean = false): String {
if (str.isEmpty()) return str

strBuilder.clear()
decomposeBuilder.clear()
if (isSeparateAll) {
str.forEachIndexed { index, c ->
strBuilder.append(if (c.isHangulLetter()) c.separate(separator) else c)
if (index != str.lastIndex) strBuilder.append(separator)
decomposeBuilder.append(if (c.isHangulLetter()) c.separate(separator) else c)
if (index != str.lastIndex) decomposeBuilder.append(separator)
}
} else {
val c = str.last()
strBuilder.append(if (c.isHangulLetter()) c.separate() else c)
decomposeBuilder.append(if (c.isHangulLetter()) c.separate() else c)
}

return strBuilder.toString()
return decomposeBuilder.toString()
}

private fun Char.separate(separator: String = ""): String {
Expand Down

0 comments on commit 2a5c057

Please sign in to comment.