# Buggy code
def _replace_consonants(word: str, consonants: str) -> str:
    _HO_HIP = "\u0e2b"  # ห
    _RO_RUA = "\u0e23"  # ร

    if not consonants:
        return word

    if len(consonants) == 1:
        return word.replace(consonants[0], _CONSONANTS[consonants[0]][0])

    i = 0
    len_cons = len(consonants)
    while i < len_cons:
        if i == 0:
            if consonants[0] == _HO_HIP:
                word = word.replace(consonants[0], "")
                del consonants[0]
                len_cons -= 1
            else:
                word = word.replace(consonants[0], _CONSONANTS[consonants[0]][0])
                i += 1
        elif consonants[i] == _RO_RUA and word[i] == _RO_RUA:
            if i + 1 == len(word):
                word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
            elif word[i + 1] == _RO_RUA:
                word = list(word)
                del word[i + 1]
                if i + 2 == len_cons:
                    word[i] = "an"
                else:
                    word[i] = "a"
                word = "".join(word)
                i += 1
            else:
                word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
                i += 1
        else:
            word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
            i += 1

    return word


# Traceback (IndexError)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-214-ffa4a144ac59> in <module>
----> 1 romanize('จรวยพร')

~/.env/lib/python3.7/site-packages/pythainlp/transliterate/core.py in romanize(text, engine)
50         from .royin import romanize
51
---> 52     return romanize(text)
53
54

~/.env/lib/python3.7/site-packages/pythainlp/transliterate/royin.py in romanize(text)
224     """
225     words = word_tokenize(text)
--> 226     romanized_words = [_romanize(word) for word in words]
227
228     return "".join(romanized_words)

~/.env/lib/python3.7/site-packages/pythainlp/transliterate/royin.py in <listcomp>(.0)
224     """
225     words = word_tokenize(text)
--> 226     romanized_words = [_romanize(word) for word in words]
227
228     return "".join(romanized_words)

~/.env/lib/python3.7/site-packages/pythainlp/transliterate/royin.py in _romanize(word)
208         word = "".join(word)
209
--> 210     word = _replace_consonants(word, consonants)
211
212     return word

~/.env/lib/python3.7/site-packages/pythainlp/transliterate/royin.py in _replace_consonants(word, consonants)
167                 )
168                 i += 1
--> 169         elif consonants[i] == _RO_RUA and word[i] == _RO_RUA:
170             if i + 1 == len(word):
171                 word = word.replace(

IndexError: string index out of range

# Fixed code
def _replace_consonants(word: str, consonants: str) -> str:
    _HO_HIP = "\u0e2b"  # ห
    _RO_RUA = "\u0e23"  # ร

    if not consonants:
        return word

    if len(consonants) == 1:
        return word.replace(consonants[0], _CONSONANTS[consonants[0]][0])

    i = 0
    len_cons = len(consonants)
    while i < len_cons:
        if i == 0:
            if consonants[0] == _HO_HIP:
                word = word.replace(consonants[0], "")
                del consonants[0]
                len_cons -= 1
            else:
                word = word.replace(consonants[0], _CONSONANTS[consonants[0]][0])
                i += 1
        elif consonants[i] == _RO_RUA and i == len(word) and word[i - 1] == _RO_RUA:
            word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
        elif consonants[i] == _RO_RUA and i < len(word):
            if i + 1 == len(word) and word[i] == _RO_RUA:
                word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
            elif word[i] == _RO_RUA and i + 1 < len(word):
                if word[i + 1] == _RO_RUA:
                    word = list(word)
                    del word[i + 1]
                    if i + 2 == len_cons:
                        word[i] = "an"
                    else:
                        word[i] = "a"
                    word = "".join(word)
                    i += 1
                elif word[i] == _RO_RUA:
                    word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
                    i += 1
                else:
                    word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
                    i += 1
            elif word[i] == _RO_RUA:
                word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
                i += 1
            else:
                word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
                i += 1
        else:
            word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
            i += 1

    return word