Wrong UTF-8 string length fixed when attaching to FString

This commit is contained in:
Markus Gans 2017-09-09 22:03:17 +02:00
parent f941da79e7
commit 1cd1e521c3
12 changed files with 94 additions and 82 deletions

View File

@ -1,3 +1,6 @@
2017-09-09 Markus Gans <guru.mail@muenster.de>
* Wrong UTF-8 string length fixed when attaching to FString
2017-09-07 Markus Gans <guru.mail@muenster.de>
* Type definition exported into a separate header file

View File

@ -735,8 +735,8 @@ int FFileDialog::changeDir (const FString& dirname)
//----------------------------------------------------------------------
void FFileDialog::printPath (const FString& txt)
{
FString path = txt;
uInt max_width = uInt(filebrowser->getWidth()) - 4;
const FString& path = txt;
const uInt max_width = uInt(filebrowser->getWidth()) - 4;
if ( path.getLength() > max_width )
filebrowser->setText(".." + path.right(max_width - 2));
@ -773,7 +773,7 @@ void FFileDialog::cb_processActivate (FWidget*, data_ptr)
if ( ! dir_entries.empty() )
{
std::vector<dir_entry>::const_iterator iter, end;
FString input = filename->getText().trim();
const FString& input = filename->getText().trim();
iter = dir_entries.begin();
end = dir_entries.end();
@ -800,12 +800,12 @@ void FFileDialog::cb_processActivate (FWidget*, data_ptr)
//----------------------------------------------------------------------
void FFileDialog::cb_processRowChanged (FWidget*, data_ptr)
{
int n = filebrowser->currentItem();
const int n = filebrowser->currentItem();
if ( n == 0 )
return;
FString name = dir_entries[uLong(n - 1)].name;
const FString& name = dir_entries[uLong(n - 1)].name;
if ( dir_entries[uLong(n - 1)].type == DT_DIR )
filename->setText( name + '/' );
@ -818,7 +818,7 @@ void FFileDialog::cb_processRowChanged (FWidget*, data_ptr)
//----------------------------------------------------------------------
void FFileDialog::cb_processClicked (FWidget*, data_ptr)
{
uLong n = uLong(filebrowser->currentItem() - 1);
const uLong n = uLong(filebrowser->currentItem() - 1);
if ( dir_entries[n].type == DT_DIR )
changeDir(dir_entries[n].name);

View File

@ -354,7 +354,6 @@ void FListView::insert (FListViewItem* item)
{
int width = (*iter).width;
bool fixed_width = (*iter).fixed_width;
FString text = (*iter).name;
if ( ! fixed_width )
{
@ -1129,7 +1128,7 @@ void FListView::drawColumnLabels()
while ( iter != header.end() )
{
FString text = (*iter).name;
const FString& text = (*iter).name;
int width = (*iter).width;
int column_width;
@ -1158,7 +1157,7 @@ void FListView::drawColumnLabels()
if ( txt_length + tailing_space < uInt(column_width) )
{
setColor();
FString line ( uInt(column_width) - tailing_space - txt_length
const FString line ( uInt(column_width) - tailing_space - txt_length
, wchar_t(fc::BoxDrawingsHorizontal) );
headerline << line; // horizontal line
}
@ -1254,6 +1253,11 @@ void FListView::drawList()
// print the entry
FString line = " ";
if ( tree_view /*&& (*iter)->expandable*/ )
{
line += "";
}
// print columns
if ( ! (*iter)->column_value.empty() )
{
@ -1261,7 +1265,8 @@ void FListView::drawList()
{
static const int leading_space = 1;
static const int ellipsis_length = 2;
FString text = (*iter)->column_value[i];
const FString& text = (*iter)->column_value[i];
int width = header[i].width;
uInt txt_length = text.getLength();
// Increment the value of i for the column position
@ -1270,23 +1275,27 @@ void FListView::drawList()
fc::text_alignment align = getColumnAlignment(int(i));
uInt align_offset = getAlignOffset (align, txt_length, uInt(width));
// Insert alignment spaces
if ( align_offset > 0 )
line += FString(align_offset, ' ');
if ( align_offset + txt_length <= uInt(width) )
{
// Insert text and tailing space
line += text.left(width);
line += FString ( leading_space + width
- int(align_offset + txt_length), ' ');
}
else if ( align == fc::alignRight )
{
// Ellipse right align text
line += FString ("..");
line += text.right(width - ellipsis_length);
line += ' ';
}
else
{
// Ellipse left align text and center text
line += text.left(width - ellipsis_length);
line += FString (".. ");
}

View File

@ -5,10 +5,10 @@ HEIGHT=16
FONTFILE="8x16graph.bdf"
(
echo -e "// newfont.h\n"
echo -e "// newfont.h\\n"
echo -e "#ifndef FNEWFONT_H"
echo -e "#define FNEWFONT_H\n"
echo -e "\nstatic unsigned char __8x16graph[] =\n{"
echo -e "#define FNEWFONT_H\\n"
echo -e "\\nstatic unsigned char __8x16graph[] =\\n{"
grep -A${HEIGHT} ^BITMAP "$FONTFILE" \
| tr '\n' ',' \
@ -26,6 +26,6 @@ FONTFILE="8x16graph.bdf"
done
echo -e "};"
echo -e "\n#endif // FNEWFONT_H"
echo -e "\\n#endif // FNEWFONT_H"
) > newfont.h

View File

@ -5,9 +5,9 @@ HEIGHT=16
FONTFILE="8x16std"
(
echo -e "// vgafont.h\n"
echo -e "// vgafont.h\\n"
echo -e "#ifndef FVGAFONT_H"
echo -e "#define FVGAFONT_H\n"
echo -e "#define FVGAFONT_H\\n"
xxd -g 1 -i -c $HEIGHT $FONTFILE \
| sed -e 's/ {$/\n{/' \
@ -24,5 +24,5 @@ FONTFILE="8x16std"
fi
done
echo -e "\n#endif // FVGAFONT_H"
echo -e "\\n#endif // FVGAFONT_H"
) > vgafont.h

View File

@ -354,7 +354,7 @@ const FString& FString::operator += (const char* s)
if ( wc_string )
{
_insert (length, uInt(std::strlen(s)), wc_string);
_insert (length, uInt(std::wcslen(wc_string)), wc_string);
delete[] wc_string;
}

View File

@ -1271,7 +1271,7 @@ void FTerm::resetXTermDefaults()
//----------------------------------------------------------------------
void FTerm::saveColorMap()
{
// ioctl (0, GIO_CMAP, &color_map);
//ioctl (0, GIO_CMAP, &color_map);
}
//----------------------------------------------------------------------
@ -2661,7 +2661,7 @@ char* FTerm::parseAnswerbackMsg (char*& current_termtype)
return 0;
}
if ( *answer_back == FString("PuTTY") )
if ( *answer_back == "PuTTY" )
{
putty_terminal = true;
@ -2726,7 +2726,7 @@ char* FTerm::parseSecDA (char*& current_termtype)
if ( num_components >= 2 )
{
FString* sec_da_components = &sec_da_split[0];
const FString* sec_da_components = &sec_da_split[0];
if ( ! sec_da_components[0].isEmpty() )
{

View File

@ -34,91 +34,91 @@ int main (int, char**)
std::cout << " instream >> " << in << std::endl;
// Test: output stream (operator <<)
FString out = L"A test string for 0 \x20ac";
const FString& out = L"A test string for 0 \x20ac";
std::cout << " outstream << " << out << std::endl;
// Test: c-string output
printf (" c_str: \"%s\"\n", out.c_str());
// Test: copy a c++ string
FString cpp_str( std::string("c++ String") );
const FString& cpp_str( std::string("c++ String") );
std::cout << " cpp_str: \"" << cpp_str << "\"" << std::endl;
// Test: copy a character
FString ch('c');
const FString& ch('c');
std::cout << " char: '" << ch << "'" << std::endl;
// Test: copy a wide character
FString wch(L'w');
const FString& wch(L'w');
std::cout << " wchar_t: '" << wch << "'" << std::endl;
// Test: utf-8 string
FString len = "длина́";
const FString& len = "длина́";
std::cout << " length: \"" << len << "\" has "
<< len.getLength() << " characters" << std::endl;
// Test: convert uppercase letter to lowercase
FString lower = FString(L"InPut").toLower();
const FString& lower = FString(L"InPut").toLower();
std::wcout << L" toLower: " << lower << std::endl;
// Test: convert lowercase letter to uppercase
FString upper = FString("inPut").toUpper();
const FString& upper = FString("inPut").toUpper();
std::cout << " toUpper: " << upper << std::endl;
// Test: concatenate two FStrings (operator +)
FString add1 = FString("FString + ") + FString("FString");
const FString& add1 = FString("FString + ") + FString("FString");
std::cout << " add: " << add1 << std::endl;
// Test: concatenate a FString and a c++ wide string (operator +)
FString add2 = FString("FString + ") + std::wstring(L"std::wstring");
const FString& add2 = FString("FString + ") + std::wstring(L"std::wstring");
std::cout << " add: " << add2 << std::endl;
// Test: concatenate a FString and a wide string (operator +)
FString add3 = FString("FString + ") + const_cast<wchar_t*>(L"wchar_t*");
const FString& add3 = FString("FString + ") + const_cast<wchar_t*>(L"wchar_t*");
std::cout << " add: " << add3 << std::endl;
// Test: concatenate a FString and a c++ string (operator +)
FString add4 = FString("FString + ") + std::string("std::string");
const FString& add4 = FString("FString + ") + std::string("std::string");
std::cout << " add: " << add4 << std::endl;
// Test: concatenate a FString and a c-string (operator +)
FString add5 = FString("FString + ") + const_cast<char*>("char*");
const FString& add5 = FString("FString + ") + const_cast<char*>("char*");
std::cout << " add: " << add5 << std::endl;
// Test: concatenate a FString and a wide character (operator +)
FString add6 = FString("FString + ") + wchar_t(L'w');
const FString& add6 = FString("FString + ") + wchar_t(L'w');
std::cout << " add: " << add6 << std::endl;
// Test: concatenate a FString and a character (operator +)
FString add7 = FString("FString + ") + char('c');
const FString& add7 = FString("FString + ") + char('c');
std::cout << " add: " << add7 << std::endl;
// Test: concatenate a character and a FString (operator +)
FString add8 = 'c' + FString(" + FString");
const FString& add8 = 'c' + FString(" + FString");
std::cout << " add: " << add8 << std::endl;
// Test: concatenate a wide character and a FString (operator +)
FString add9 = L'w' + FString(" + FString");
const FString& add9 = L'w' + FString(" + FString");
std::cout << " add: " << add9 << std::endl;
// Test: concatenate a c-string and a FString (operator +)
FString add10 = const_cast<char*>("char*") + FString(" + FString");
const FString& add10 = const_cast<char*>("char*") + FString(" + FString");
std::cout << " add: " << add10 << std::endl;
// Test: concatenate a c++ string and a FString (operator +)
FString add11 = std::string("std::string") + FString(" + FString");
const FString& add11 = std::string("std::string") + FString(" + FString");
std::cout << " add: " << add11 << std::endl;
// Test: concatenate a wide string and a FString (operator +)
FString add12 = const_cast<wchar_t*>(L"wchar_t*") + FString(" + FString");
const FString& add12 = const_cast<wchar_t*>(L"wchar_t*") + FString(" + FString");
std::cout << " add: " << add12 << std::endl;
// Test: concatenate a c++ wide string and a FString (operator +)
FString add13 = std::wstring(L"std::wstring") + FString(" + FString");
const FString& add13 = std::wstring(L"std::wstring") + FString(" + FString");
std::cout << " add: " << add13 << std::endl;
// Test: compare operators ==, <=, <, >=, >, !=
FString cmp = "compare";
const FString& cmp = "compare";
if ( cmp == FString("compare") )
std::cout << " cmp: == Ok" << std::endl;
@ -172,7 +172,7 @@ int main (int, char**)
// Test: convert a string to a unsigned long interger
try
{
uLong ulong_num = FString("123456789").toULong();
const uLong ulong_num = FString("123456789").toULong();
std::cout << " toULong: " << ulong_num << std::endl;
}
catch (const std::invalid_argument& ex)
@ -187,7 +187,7 @@ int main (int, char**)
// Test: convert a string to a signed long interger
try
{
long long_num = FString("-9876543210").toLong();
const long long_num = FString("-9876543210").toLong();
std::cout << " toLong: " << long_num << std::endl;
}
catch (const std::invalid_argument& ex)
@ -204,7 +204,7 @@ int main (int, char**)
try
{
double double_num = FString("2.7182818284590452353").toDouble();
const double double_num = FString("2.7182818284590452353").toDouble();
std::ios_base::fmtflags save_flags = std::cout.flags();
std::cout << " toDouble: " << std::setprecision(11)
<< double_num << std::endl;
@ -249,7 +249,7 @@ int main (int, char**)
<< fnum2 << " (signed)" << std::endl;
// Test: remove whitespace from the end of a string
FString trim_str = " A string \t";
const FString& trim_str = " A string \t";
std::wcout << " rtrim: \""
<< trim_str.rtrim() << "\"" << std::endl;
@ -262,7 +262,7 @@ int main (int, char**)
<< trim_str.trim() << "\"" << std::endl;
// Test: 11 characters from the left of the string
FString alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
const FString& alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
std::cout << " left: \""
<< alphabet.left(11) << "\"" << std::endl;
@ -304,7 +304,7 @@ int main (int, char**)
}
// Test: character access with std::iterator
FString stringIterator = "iterator";
const FString& stringIterator = "iterator";
FString::iterator iter;
iter = stringIterator.begin();
std::cout << " " << stringIterator << ": ";
@ -353,22 +353,22 @@ int main (int, char**)
// Test: find and replace a substring
FString source_str = "computer and software";
FString replace_str = source_str.replace("computer", "hard-");
const FString& replace_str = source_str.replace("computer", "hard-");
std::cout << " replace: "
<< replace_str << std::endl;
// Test: convert tabs to spaces
FString tab_str = "1234\t5678";
const FString& tab_str = "1234\t5678";
std::cout << " tab: "
<< tab_str.expandTabs() << std::endl;
// Test: backspaces remove characters in the string
FString bs_str = "t\b\bTesT\bt";
const FString& bs_str = "t\b\bTesT\bt";
std::cout << "backspace: "
<< bs_str.removeBackspaces() << std::endl;
// Test: delete characters remove characters in the string
FString del_str = "apple \177\177\177pietree";
const FString& del_str = "apple \177\177\177pietree";
std::cout << " delete: "
<< del_str.removeDel() << std::endl;
}

View File

@ -75,13 +75,13 @@ smallWindow::smallWindow (FWidget* parent)
right_arrow->ignorePadding();
right_arrow->setGeometry (getWidth() - 1, 2, 1, 1);
FString top_left_label_text = "menu";
const FString& top_left_label_text = "menu";
top_left_label = new FLabel (top_left_label_text, this);
top_left_label->setForegroundColor (wc.label_inactive_fg);
top_left_label->setEmphasis();
top_left_label->setGeometry (1, 1, 6, 1);
FString top_right_label_text = "zoom";
const FString& top_right_label_text = "zoom";
top_right_label = new FLabel (top_right_label_text, this);
top_right_label->setAlignment (fc::alignRight);
top_right_label->setForegroundColor (wc.label_inactive_fg);