36void G::Environment::sanitise( Map & map )
38 for(
auto p = map.begin() ; p != map.end() ; )
40 if( (*p).first.empty() || (*p).first.find(
'\0') != std::string::npos || (*p).second.find(
'\0') != std::string::npos )
50 if( !key.empty() && key.find(
'\0') == std::string::npos && value.find(
'\0') == std::string::npos )
52 m_map[sv_to_string(key)] = sv_to_string(value) ;
64 return m_map.find(name) != m_map.end() ;
70 return contains(name) ? (*m_map.find(name)).second : default_ ;
76 std::size_t n = std::accumulate( m_map.begin() , m_map.end() , std::size_t(0U) ,
77 [](std::size_t n_,
const Map::value_type &p_){return n_+p_.first.size()+p_.second.size()+2U;} ) ;
79 result.reserve( n+1U ) ;
80 for(
const auto & p : m_map )
81 result.append(p.first).append(1U,
'=').append(p.second).append(1U,
'\0') ;
82 result.append( 1U ,
'\0' ) ;
89 std::size_t n = std::accumulate( m_map.begin() , m_map.end() , std::size_t(0U) ,
90 [](std::size_t n_,
const Map::value_type &p_){return n_+p_.first.size()+p_.second.size()+2U;} ) ;
92 result.reserve( n+1U ) ;
93 for(
const auto & p : m_map )
94 result.append(fn(std::string(p.first).append(1U,L
'=').append(p.second))).append(1U,L
'\0') ;
95 result.append( 1U , L
'\0' ) ;
102 G_ASSERT( block.size() >= 2U && block[block.size()-1U] == 0 && block[block.size()-2U] == 0 ) ;
103 std::vector<char*> result ;
104 if( block.size() >= 2U && block[block.size()-1U] == 0 && block[block.size()-2U] == 0 )
106 result.reserve( std::count( block.begin() , block.end() , 0 ) ) ;
107 const char *
const end = block.data() + block.size() ;
108 for(
const char * p = block.data() ; p < end && *p ; p += (std::strlen(p)+1U) )
109 result.push_back(
const_cast<char*
>(p) ) ;
111 result.push_back(
nullptr ) ;
std::string value(const std::string &name, const std::string &default_={}) const
Returns the value of the given variable in this set.
Environment(const std::map< std::string, std::string > &)
Constructor from a map.
std::string block() const
Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte a...
bool contains(const std::string &name) const
Returns true if the given variable is in this set.
static std::vector< char * > array(const std::string &block)
Returns a pointer array pointing into the given block(), with const-casts.
bool add(std::string_view key, std::string_view value)
Adds an environment variable. Returns false if invalid.