def parse(self, raw_container: CharacterContainer) -> List[CharacterContainer]: words = [] index = 0 self._have_words_started = False container = CharacterContainer() while True: if index == len(raw_container.characters): break character: Character = raw_container.characters[index] is_literal = False valid_literal = None for literal in self.literals: for i, c in enumerate(literal): if i + index < len( raw_container.characters ) and c == raw_container.characters[i + index].character: is_literal = True else: is_literal = False break if is_literal: valid_literal = literal break if self._has_seen_first_character(raw_container, index) \ and (self._is_new_word(raw_container, index) or self._is_series_of_delimiters(raw_container, index)) \ and len(container.characters) > 0: words.append(container) container = CharacterContainer() if valid_literal: for c in valid_literal: container.append_str(c.lower()) index += 1 self._have_words_started = True if index < len(raw_container.characters): words.append(container) container = CharacterContainer() character: Character = raw_container.characters[index] else: break container.append_str( character.character.lower() if character.case == CharacterType.UPPERCASE else character.character) index += 1 words.append(container) return words
def format(self, words: List[CharacterContainer]) -> str: format_literals = [ acronym.lower() for acronym in self._format_literals ] case_delimited = CharacterContainer() first_character_casing = self._first_character_casing valid_first_characters = [ CharacterType.UPPERCASE, CharacterType.LOWERCASE, CharacterType.OTHER ] should_capitalize = False is_first_word = True for word in words: if str(word) in format_literals: case_delimited.append_str( self._format_literals[format_literals.index(str(word))]) is_first_word = False should_capitalize = True else: for character in word: if is_first_word and character.case in valid_first_characters: is_first_word = False if first_character_casing == CharacterType.UPPERCASE: case_delimited.append_str( character.character.upper()) elif first_character_casing == CharacterType.LOWERCASE: case_delimited.append_str( character.character.lower()) elif should_capitalize: case_delimited.append_str(character.character.upper()) should_capitalize = False else: case_delimited.append_str(character.character) should_capitalize = True and not is_first_word return str(case_delimited)