1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.util.stdext..string;
26 
27 import std.string;
28 import std.array;
29 import std.algorithm;
30 
31 import des.util.data.type : ArrayData, AlienArray;
32 
33 @trusted pure
34 {
35     ///
36     string toSnakeCase( in string str, bool ignore_first=true ) @property
37     {
38         string[] buf;
39         buf ~= "";
40         foreach( i, ch; str )
41         {
42             if( [ch].toUpper == [ch] ) buf ~= "";
43             buf[$-1] ~= [ch].toLower;
44         }
45         if( buf[0].length == 0 && ignore_first )
46             buf = buf[1..$];
47         return buf.join("_");
48     }
49 
50     ///
51     string toCamelCaseBySep( in string str, string sep="_", bool first_capitalize=true )
52     {
53         auto arr = array( filter!"a.length > 0"( str.split(sep) ) );
54         string[] ret;
55         foreach( i, v; arr )
56         {
57             auto bb = v.capitalize;
58             if( i == 0 && !first_capitalize )
59                 bb = v.toLower;
60             ret ~= bb;
61         }
62         return ret.join("");
63     }
64 
65     ///
66     string toCamelCase( in string str, bool first_capitalize=true ) @property
67     { return toCamelCaseBySep( str, "_", first_capitalize ); }
68 
69     /// copy chars to string
70     string toDString( const(char*) c_str ) nothrow
71     {
72         if( c_str is null ) return "";
73         char *ch = cast(char*)c_str;
74         size_t n;
75         while( *ch != '\0' ) n++;
76         return AlienArray!char( ArrayData( n, cast(size_t)c_str ) ).arr.idup;
77     }
78 
79     /// ditto
80     string toDStringFix(size_t S)( const(char[S]) c_buf ) nothrow
81     {
82         size_t n;
83         foreach( c; c_buf )
84         {
85             if( c == '\0' ) break;
86             n++;
87         }
88         return AlienArray!char( ArrayData( n, cast(size_t)c_buf.ptr ) ).arr.idup;
89     }
90 }
91 
92 ///
93 unittest
94 {
95     assert( "someVar".toSnakeCase == "some_var" );
96     assert( "SomeVar".toSnakeCase == "some_var" );
97     assert( "SomeVar".toSnakeCase(false) == "_some_var" );
98     assert( "ARB".toSnakeCase == "a_r_b" );
99     assert( "ARB".toSnakeCase(false) == "_a_r_b" );
100 }
101 
102 ///
103 unittest
104 {
105     assert( "some_class".toCamelCase == "SomeClass" );
106     assert( "_some_class".toCamelCase == "SomeClass" );
107     assert( "some_func".toCamelCase(false) == "someFunc" );
108     assert( "_some_func".toCamelCase(false) == "someFunc" );
109     assert( "a_r_b".toCamelCase == "ARB" );
110     assert( toCamelCase( "program_build" ) == "ProgramBuild" );
111     assert( toCamelCaseBySep( "single-precision-constant", "-", false ) == "singlePrecisionConstant" );
112 }