diff --git a/ContactsViewModel.cpp b/ContactsViewModel.cpp index 1519fad990dd88f599844a8ec2f3aa6698d7a4d4..12dde6602902b556ab7c31ffc8c1a91ba659207e 100644 --- a/ContactsViewModel.cpp +++ b/ContactsViewModel.cpp @@ -71,8 +71,9 @@ ContactsViewModel::findContactByName(String^ name) Contact^ ContactsViewModel::addNewContact(String^ name, String^ ringId) { - if (contactsList_ && !findContactByName(name)) { - Contact^ contact = ref new Contact(name, name); + auto trimedName = Utils::Trim(name); + if (contactsList_ && !findContactByName(trimedName)) { + Contact^ contact = ref new Contact(trimedName, trimedName); contactsList_->Append(contact); saveContactsToFile(); return contact; diff --git a/Utils.h b/Utils.h index 05d54270b6132b02c7aecc2af3d95c7cbc611bd9..7608643baa19e7328738c75f56c971e30563d07a 100644 --- a/Utils.h +++ b/Utils.h @@ -74,5 +74,19 @@ Platform::String^ toPlatformString(const std::string& str) return ref new Platform::String(wsstr.c_str(), wsstr.length()); } +Platform::String^ Trim(Platform::String^ s) +{ + const WCHAR* first = s->Begin(); + const WCHAR* last = s->End(); + + while (first != last && iswspace(*first)) + ++first; + + while (first != last && iswspace(last[-1])) + --last; + + return ref new Platform::String(first, static_cast<unsigned int>(last - first)); +} + } } \ No newline at end of file