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.math.util.accessstring;
26 
27 import std.string;
28 import std.algorithm;
29 import std.stdio;
30 
31 pure bool isCompatibleArrayAccessString( size_t N, string str, string sep="" )
32 { return N == getAccessFieldsCount(str,sep) && isArrayAccessString(str,sep); }
33 
34 pure bool isArrayAccessString( in string as, in string sep="", bool allowDot=false )
35 {
36     if( as.length == 0 ) return false;
37     auto splt = as.split(sep);
38     foreach( i, val; splt )
39         if( !isValueAccessString(val,allowDot) || canFind(splt[0..i],val) )
40             return false;
41     return true;
42 }
43 
44 pure size_t getAccessFieldsCount( string str, string sep )
45 { return str.split(sep).length; }
46 
47 pure ptrdiff_t getIndex( string str, string arg, string sep="" )
48 {
49     foreach( i, v; str.split(sep) )
50         if( arg == v ) return i;
51     return -1;
52 }
53 
54 pure bool oneOfAccess( string str, string arg, string sep="" )
55 {
56     auto splt = str.split(sep);
57     return canFind(splt,arg);
58 }
59 
60 pure bool oneOfAccessAll( string str, string arg, string sep="" )
61 {
62     auto splt = arg.split("");
63     return all!(a=>oneOfAccess(str,a,sep))(splt);
64 }
65 
66 pure bool isOneSymbolPerFieldAccessString( string str, string sep="" )
67 {
68     auto splt = str.split(sep);
69     foreach( s; splt )
70         if( s.length > 1 ) return false;
71     return true;
72 }
73 
74 unittest
75 {
76     assert( isValueAccessString( "hello" ) );
77     assert( isValueAccessString( "x" ) );
78     assert( isValueAccessString( "_ok" ) );
79     assert( isValueAccessString( "__ok" ) );
80     assert( isValueAccessString( "__o1k" ) );
81     assert( isValueAccessString( "_2o1k3" ) );
82     assert( !isValueAccessString( "0__ok" ) );
83     assert( !isValueAccessString( "__o-k" ) );
84     assert( isArrayAccessString( "xyz" ) );
85     assert( isArrayAccessString( "x|dx|y|dy", "|" ) );
86     assert( isCompatibleArrayAccessString( 4, "x|dx|y|dy", "|" ) );
87     assert( isCompatibleArrayAccessString( 3, "xyz" ) );
88     assert( !isCompatibleArrayAccessString( 4, "xxxy" ) );
89     assert( !isCompatibleArrayAccessString( 3, "xxx" ) );
90     static assert( getIndex( "x y z", "x", " " ) == 0 );
91     static assert( getIndex( "x y z", "y", " " ) == 1 );
92     static assert( getIndex( "x y z", "z", " " ) == 2 );
93     assert( getIndex( "x|dx|y|dy", "dx", "|" ) == 1 );
94     assert( getIndex( "x|dx|y|dy", "1dx", "|" ) == -1 );
95 
96     assert( oneOfAccessAll("xyz","xy") );
97     assert( oneOfAccessAll("xyz","yx") );
98     assert( oneOfAccessAll("xyz","xxxxyxyyyz") );
99     assert( oneOfAccessAll("x,y,z","xxxxyxyyyz",",") );
100     assert( isOneSymbolPerFieldAccessString("xyz") );
101     assert( isOneSymbolPerFieldAccessString("x,y,z",",") );
102 
103     assert( !isArrayAccessString("x.y.z","",false) );
104     assert( !isArrayAccessString("x.y.z","",true) );
105     assert( !isArrayAccessString("x.y.z"," ",false) );
106     assert(  isArrayAccessString("x.y.z"," ",true) );
107     assert(  isArrayAccessString("pos.x pos.y pos.z vel.x vel.y vel.z"," ",true) );
108 
109     assert(  isArrayAccessString( "pos vel", " ", true ) );
110     assert(  isArrayAccessString( "abcd", " ", true ) );
111     assert(  isArrayAccessString( "a1 a2", " ", true ) );
112     assert(  isArrayAccessString( "ok.no", " ", true ) );
113     auto fstr = "pos.x pos.y vel.x vel.y";
114     assert(  isArrayAccessString( fstr, " ", true ) );
115     assert( !isArrayAccessString( fstr[0 .. $-1], " ", true ) );
116     assert( !isArrayAccessString( "ok.1", " ", true ) );
117     assert( !isArrayAccessString( "1abcd", " ", true ) );
118     assert( !isArrayAccessString( "not 2ok", " ", true ) );
119 }
120 
121 pure
122 {
123 
124 bool isValueAccessString( in string as, bool allowDot=false )
125 {
126     return as.length > 0 &&
127     startsWithAllowedChars(as) &&
128     (allowDot?(all!(a=>isValueAccessString(a))(as.split("."))):allowedCharsOnly(as));
129 }
130 
131 bool startsWithAllowedChars( in string as )
132 {
133     switch(as[0])
134     {
135         case 'a': .. case 'z': goto case;
136         case 'A': .. case 'Z': goto case;
137         case '_': return true;
138         default: return false;
139     }
140 }
141 
142 bool allowedCharsOnly( in string as )
143 {
144     foreach( c; as ) if( !allowedChar(c) ) return false;
145     return true;
146 }
147 
148 bool allowedChar( in char c )
149 {
150     switch(c)
151     {
152         case 'a': .. case 'z': goto case;
153         case 'A': .. case 'Z': goto case;
154         case '0': .. case '9': goto case;
155         case '_': return true;
156         default: return false;
157     }
158 }
159 
160 }