StringHelper.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include "icl_core/StringHelper.h"
00024 
00025 namespace icl_core {
00026 
00027 std::vector<String> split(const String& str, const String& delimiter)
00028 {
00029   String s = str;
00030   String::size_type pos = 0;
00031   std::vector<String> substrings;
00032   if (s.empty())
00033   {
00034     substrings.push_back("");
00035     return substrings;
00036   }
00037   while ((pos = s.find(delimiter)) != String::npos)
00038   {
00039     substrings.push_back(s.substr(0, pos));
00040     s.erase(0, pos+delimiter.size());
00041   }
00042   if (!s.empty())
00043   {
00044     substrings.push_back(s);
00045   }
00046   return substrings;
00047 }
00048 
00049 String join(const std::vector<String>& substrings, const String& delimiter)
00050 {
00051   String result;
00052   for (std::vector<String>::const_iterator it = substrings.begin(); it != substrings.end(); ++it)
00053   {
00054     if (it != substrings.begin())
00055     {
00056       result += delimiter;
00057     }
00058     result += *it;
00059   }
00060   return result;
00061 }
00062 
00063 String trim(String const & str)
00064 {
00065   std::string result = "";
00066 
00067   std::string::size_type length = str.length();
00068 
00069   std::string::size_type trim_front = 0;
00070   while ((trim_front < length) && isspace(static_cast<unsigned char>(str[trim_front])))
00071   {
00072     ++trim_front;
00073   }
00074 
00075   std::string::size_type trim_end = length - 1;
00076   while ((trim_end > trim_front) && isspace(static_cast<unsigned char>(str[trim_end])))
00077   {
00078     --trim_end;
00079   }
00080 
00081   if (trim_front == length)
00082   {
00083     result = "";
00084   }
00085   else
00086   {
00087     result = str.substr(trim_front, trim_end - trim_front + 1);
00088   }
00089 
00090   return result;
00091 }
00092 
00094 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00095 
00096 String Trim(String const & str)
00097 {
00098   return trim(str);
00099 }
00100 
00101 #endif
00102 
00103 
00104 }


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24