@@ -914,32 +914,71 @@ static void string_to_cells(string_view Str, size_t& CharsConsumed, cells& Cells
914914void chars_to_cells (string_view Str, size_t & CharsConsumed, size_t const CellsAvailable, size_t & CellsConsumed)
915915{
916916 cells Cells;
917- const auto & CellsToBeConsumed = Cells.emplace <0 >();
917+ const auto & CellsToBeConsumed = Cells.emplace <size_t >();
918918 string_to_cells (Str, CharsConsumed, Cells, CellsAvailable);
919919 CellsConsumed = CellsToBeConsumed;
920920
921- #ifdef _DEBUG
922- if (CharsConsumed == Str.size ())
923- assert (CellsConsumed == visual_string_length (Str));
924- #endif
921+ assert (CharsConsumed < Str.size () || CellsConsumed == visual_string_length (Str));
925922}
926923
927- size_t Text (string_view Str, size_t const CellsAvailable)
924+ // Does not advance CurX
925+ static void write_text (string_view Str, size_t & CharsConsumed, size_t const CellsAvailable, size_t & CellsConsumed)
928926{
929- if (Str. empty ())
930- return 0 ;
927+ CharsConsumed = 0 ;
928+ CellsConsumed = 0 ;
931929
932- cells Cells;
933- const auto & Buffer = Cells.emplace <1 >();
930+ if (Str.empty ()) return ;
934931
935- size_t CharsConsumed = 0 ;
932+ cells Cells;
933+ const auto & Buffer = Cells.emplace <real_cells>();
936934
937935 string_to_cells (Str, CharsConsumed, Cells, CellsAvailable);
938936
939937 Global->ScrBuf ->Write (CurX, CurY, Buffer);
940- CurX += static_cast <int >(Buffer.size ());
941938
942- return Buffer.size ();
939+ CellsConsumed = Buffer.size ();
940+ }
941+
942+ // Returns true if all cells were consumed
943+ bool ClippedText (string_view Str, const segment Bounds, bool & AllCharsConsumed)
944+ {
945+ const auto WriteOrSkip{ [&Str](const auto RightBoundary, const auto Operation)
946+ {
947+ if (Str.empty () || CurX >= RightBoundary) return ;
948+
949+ size_t CharsConsumed{};
950+ size_t CellsConsumed{};
951+ Operation (Str, CharsConsumed, RightBoundary - CurX, CellsConsumed);
952+ Str = Str.substr (CharsConsumed);
953+ CurX += static_cast <int >(CellsConsumed);
954+ } };
955+
956+ WriteOrSkip (Bounds.start (), chars_to_cells);
957+ WriteOrSkip (Bounds.end (), write_text);
958+
959+ AllCharsConsumed = Str.empty ();
960+ if (AllCharsConsumed || CurX >= Bounds.end ())
961+ return CurX >= Bounds.end ();
962+
963+ assert (char_width::is_wide (encoding::utf16::extract_codepoint (Str)));
964+ assert (CurX == Bounds.end () - 1 );
965+ return true ;
966+ }
967+
968+ // Returns true if all cells were consumed
969+ bool ClippedText (string_view Str, const segment Bounds)
970+ {
971+ bool AllCharsConsumed{};
972+ return ClippedText (Str, Bounds, AllCharsConsumed);
973+ }
974+
975+ size_t Text (string_view Str, size_t const CellsAvailable)
976+ {
977+ size_t CharsConsumed{};
978+ size_t CellsConsumed{};
979+ write_text (Str, CharsConsumed, CellsAvailable, CellsConsumed);
980+ CurX += static_cast <int >(CellsConsumed);
981+ return CellsConsumed;
943982}
944983
945984size_t Text (string_view Str)
0 commit comments