summaryrefslogtreecommitdiff
path: root/glib/glibmm/stringutils.cc
blob: e72f029d3db777251f18fb401747d8fee8724fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// -*- c++ -*-
/* $Id$ */

/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmmconfig.h>
#include <glibmm/stringutils.h>
#include <glibmm/utility.h>
#include <glib.h>
#include <cerrno>
#include <stdexcept>

bool Glib::str_has_prefix(const std::string& str, const std::string& prefix)
{
  return g_str_has_prefix(str.c_str(), prefix.c_str());
}

bool Glib::str_has_suffix(const std::string& str, const std::string& suffix)
{
  return g_str_has_suffix(str.c_str(), suffix.c_str());
}

double Glib::Ascii::strtod(const std::string& str)
{
  std::string::size_type dummy;
  return Glib::Ascii::strtod(str, dummy, 0);
}

double Glib::Ascii::strtod(const std::string&      str,
                           std::string::size_type& end_index,
                           std::string::size_type  start_index)
{
  if(start_index >= str.size())
  {
    throw std::out_of_range("out of range (strtod): start_index > str.size()");
  }

  const char *const bufptr = str.c_str();
  char* endptr = nullptr;

  const double result = g_ascii_strtod(bufptr + start_index, &endptr);
  const int    err_no = errno;

  if(err_no != 0)
  {
    g_return_val_if_fail(err_no == ERANGE, result);

    //Interpret the result in the event of an error:
    if(result > 0.0)
      throw std::overflow_error("overflow (strtod): positive number too large");

    if(result < 0.0)
      throw std::overflow_error("overflow (strtod): negative number too large");

    throw std::underflow_error("underflow (strtod): number too small");
  }

  if(endptr)
    end_index = endptr - bufptr;
  else
    end_index = str.size();

  return result;
}

std::string Glib::Ascii::dtostr(double d)
{
  char buf[G_ASCII_DTOSTR_BUF_SIZE];

  return g_ascii_dtostr(buf, sizeof(buf), d);
}

std::string Glib::strescape(const std::string& source)
{
  const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), 0));
  return buf.get();
}

std::string Glib::strescape(const std::string& source, const std::string& exceptions)
{
  const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), exceptions.c_str()));
  return buf.get();
}

std::string Glib::strcompress(const std::string& source)
{
  const Glib::ScopedPtr<char> buf (g_strcompress(source.c_str()));
  return buf.get();
}

Glib::ustring Glib::strerror(int errnum)
{
  return g_strerror(errnum);
}

Glib::ustring Glib::strsignal(int signum)
{
  return g_strsignal(signum);
}