def _build_attributed_string(self, chars):
        """
        Build attributed text in a more efficient way than char by char.
        It groups characters with the same attributes and apply the attributes
        to them at once.
        :param [ShChar] chars: A list of ShChar upon which the attributed text is built.
        :rtype: object
        """
        # Initialize a string with default attributes
        attributed_text = NSMutableAttributedString.alloc().initWithString_attributes_(
            ''.join(char.data for char in chars),
            self._build_attributes(DEFAULT_CHAR),
        ).autorelease()

        prev_char = chars[0]
        location = length = 0
        for idx, curr_char in enumerate(chars):
            length += 1
            if not ShChar.same_style(prev_char, curr_char):  # a group is found
                if not ShChar.same_style(prev_char, DEFAULT_CHAR):  # skip default attrs
                    attributed_text.setAttributes_range_(self._build_attributes(prev_char), (location, length - 1))
                length = 1
                location = idx
                prev_char = curr_char

            if idx == len(chars) - 1:  # last char
                if not ShChar.same_style(prev_char, DEFAULT_CHAR):
                    attributed_text.setAttributes_range_(self._build_attributes(prev_char), (location, length))

        return attributed_text

        Build attributed text in a more efficient way than char by char.
        It groups characters with the same attributes and apply the attributes
        to them at once.
        :param [ShChar] chars: A list of ShChar upon which the attributed text is built.
        :rtype: object
        
system/shui/pythonista_ui.py