Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stackstring.hpp « nowide « boost « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 948a22f7f630fb8debe8ff8985829f6f53ff787a (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
//  Copyright (c) 2012 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_NOWIDE_DETAILS_WIDESTR_H_INCLUDED
#define BOOST_NOWIDE_DETAILS_WIDESTR_H_INCLUDED
#include <boost/nowide/convert.hpp>
#include <string.h>
#include <algorithm>

namespace boost {
namespace nowide {

///
/// \brief A class that allows to create a temporary wide or narrow UTF strings from
/// wide or narrow UTF source.
///
/// It uses on stack buffer of the string is short enough
/// and allocated a buffer on the heap if the size of the buffer is too small
///    
template<typename CharOut=wchar_t,typename CharIn = char,size_t BufferSize = 256>
class basic_stackstring {
public:
   
    static const size_t buffer_size = BufferSize; 
    typedef CharOut output_char;
    typedef CharIn input_char;

    basic_stackstring(basic_stackstring const &other) : 
    mem_buffer_(0)
    {
        clear();
        if(other.mem_buffer_) {
            size_t len = 0;
            while(other.mem_buffer_[len])
                len ++;
            mem_buffer_ = new output_char[len + 1];
            memcpy(mem_buffer_,other.mem_buffer_,sizeof(output_char) * (len+1));
        }
        else {
            memcpy(buffer_,other.buffer_,buffer_size * sizeof(output_char));
        }
    }
    
    void swap(basic_stackstring &other)
    {
        std::swap(mem_buffer_,other.mem_buffer_);
        for(size_t i=0;i<buffer_size;i++)
            std::swap(buffer_[i],other.buffer_[i]);
    }
    basic_stackstring &operator=(basic_stackstring const &other)
    {
        if(this != &other) {
            basic_stackstring tmp(other);
            swap(tmp);            
        }
        return *this;
    }

    basic_stackstring() : mem_buffer_(0)
    {
    }
    bool convert(input_char const *input)
    {
        return convert(input,details::basic_strend(input));
    }
    bool convert(input_char const *begin,input_char const *end)
    {
        clear();

        size_t space = get_space(sizeof(input_char),sizeof(output_char),end - begin) + 1;
        if(space <= buffer_size) {
            if(basic_convert(buffer_,buffer_size,begin,end))
                return true;
            clear();
            return false;
        }
        else {
            mem_buffer_ = new output_char[space];
            if(!basic_convert(mem_buffer_,space,begin,end)) {
                clear();
                return false;
            }
            return true;
        }

    }
    output_char *c_str()
    {
        if(mem_buffer_)
            return mem_buffer_;
        return buffer_;
    }
    output_char const *c_str() const
    {
        if(mem_buffer_)
            return mem_buffer_;
        return buffer_;
    }
    void clear()
    {
        if(mem_buffer_) {
            delete [] mem_buffer_;
            mem_buffer_=0;
        }
        buffer_[0] = 0;
    }
    ~basic_stackstring()
    {
        clear();
    }
private:
    static size_t get_space(size_t insize,size_t outsize,size_t in)
    {
        if(insize <= outsize)
            return in;
        else if(insize == 2 && outsize == 1) 
            return 3 * in;
        else if(insize == 4 && outsize == 1) 
            return 4 * in;
        else  // if(insize == 4 && outsize == 2) 
            return 2 * in;
    }
    output_char buffer_[buffer_size];
    output_char *mem_buffer_;
};  //basic_stackstring

///
/// Convinience typedef
///
typedef basic_stackstring<wchar_t,char,256> wstackstring;
///
/// Convinience typedef
///
typedef basic_stackstring<char,wchar_t,256> stackstring;
///
/// Convinience typedef
///
typedef basic_stackstring<wchar_t,char,16> wshort_stackstring;
///
/// Convinience typedef
///
typedef basic_stackstring<char,wchar_t,16> short_stackstring;


} // nowide
} // namespace boost

#endif
///
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4