From 2647eaaff7856232682ee261d8d5a32f78d66c39 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Wed, 31 Aug 2016 12:08:15 -0400
Subject: [PATCH] contact : trim contact white space

- removes the white spaces from the begin and the end of contacts
during their add.

Change-Id: I6d01d84cdd68c6b044da8b16c6ee89fbf9e721e8
Tuleap: #979
---
 ContactsViewModel.cpp |  5 +++--
 Utils.h               | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/ContactsViewModel.cpp b/ContactsViewModel.cpp
index 1519fad..12dde66 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 05d5427..7608643 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
-- 
GitLab