]> git.tdb.fi Git - libs/core.git/blobdiff - source/strings/utils.cpp
Add some utility functions for joining strings
[libs/core.git] / source / strings / utils.cpp
index 5728332d595c1bba62d126f9ddbee776c6164004..80e59f07a6cb5802eb741aa870a0498e2308fea0 100644 (file)
@@ -1,13 +1,7 @@
-/* $Id$
-
-This file is part of libmspstrings
-Copyright © 2006-2008 Mikko Rasa
-Distributed under the LGPL
-*/
-
 #include <algorithm>
 #include <list>
 #include <algorithm>
 #include <list>
-#include <msp/core/except.h>
+#include <stdexcept>
+#include <cctype>
 #include "utils.h"
 
 using namespace std;
 #include "utils.h"
 
 using namespace std;
@@ -136,6 +130,20 @@ string strip(const string &s)
        return result;
 }
 
        return result;
 }
 
+string &append(string &str, const string &sep, const string &other)
+{
+       if(!str.empty() && !other.empty())
+               str += sep;
+       str += other;
+       return str;
+}
+
+string join(const string &str1, const string &sep, const string &str2)
+{
+       string result = str1;
+       return append(result, sep, str2);
+}
+
 string c_unescape(const std::string &str)
 {
        bool escape = false;
 string c_unescape(const std::string &str)
 {
        bool escape = false;
@@ -155,7 +163,7 @@ string c_unescape(const std::string &str)
                        else if(*i>='A' && *i<='F')
                                digit = *i-'A'+10;
                        else
                        else if(*i>='A' && *i<='F')
                                digit = *i-'A'+10;
                        else
-                               throw InvalidParameterValue("Invalid hexadecimal digit");
+                               throw invalid_argument("c_unescape");
 
                        numeric_value = (numeric_value<<4 | digit);
                        ++numeric_pos;
 
                        numeric_value = (numeric_value<<4 | digit);
                        ++numeric_pos;
@@ -171,7 +179,7 @@ string c_unescape(const std::string &str)
                        if(*i>='0' && *i<='7')
                                digit = *i-'0';
                        else
                        if(*i>='0' && *i<='7')
                                digit = *i-'0';
                        else
-                               throw InvalidParameterValue("Invalid octal digit");
+                               throw invalid_argument("c_unescape");
 
                        numeric_value = (numeric_value<<3 | digit);
                        ++numeric_pos;
 
                        numeric_value = (numeric_value<<3 | digit);
                        ++numeric_pos;
@@ -216,7 +224,7 @@ string c_unescape(const std::string &str)
                        else if(*i=='\\')
                                result += '\\';
                        else
                        else if(*i=='\\')
                                result += '\\';
                        else
-                               throw InvalidParameterValue("Invalid escape sequence");
+                               throw invalid_argument("c_unescape");
 
                        escape = false;
                }
 
                        escape = false;
                }
@@ -227,7 +235,7 @@ string c_unescape(const std::string &str)
        }
 
        if(escape)      
        }
 
        if(escape)      
-               throw InvalidParameterValue("Stray backslash at end of string");
+               throw invalid_argument("c_unescape");
 
        return result;
 }
 
        return result;
 }
@@ -260,7 +268,9 @@ string c_escape(const string &str, bool escape_8bit)
                        result += "\\\\";
                else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
                {
                        result += "\\\\";
                else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
                {
-                       char buf[4] = {'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)};
+                       char buf[4] = { '\\', 0 };
+                       for(unsigned j=0; j<3; ++j)
+                               buf[1+j] = '0'+((static_cast<unsigned char>(*i)>>(6-j*3))&7);
                        result.append(buf, 4);
                }
                else
                        result.append(buf, 4);
                }
                else