Qore Util Module Reference  1.3
 All Classes Namespaces Functions Variables Groups Pages
Util.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file Util.qm Qore user module for generally useful routines
3 
4 /* Util.qm Copyright 2014 - 2017 Qore Technologies, s.r.o.
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is 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
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.13 or better
26 
27 // do not use $ for vars, assume local var scope
28 
29 // require type definitions everywhere
30 
31 // enable all warnings
32 
33 }
34 
116 namespace Init {
117  init();
118 
119 };
120 
122 namespace Util {
124 
126  public struct UriQueryInfo {
128  string method;
129 
132  };
133 
135 
148  int compare_version(string lv, string rv);
149 
150 
152 
170  string normalize_dir(string dir, string cwd = getcwd());
171 
172 
174 
189  string normalize_dir_unix(string dir, string cwd = getcwd());
190 
191 
193 
208  string normalize_dir_windows(string dir, string cwd = getcwd());
209 
210 
212 
225  bool absolute_path(string path);
226 
227 
229 
242  bool absolute_path_unix(string path);
243 
244 
246 
259  bool absolute_path_windows(string path);
260 
261 
262  // private, non-exported constant hashes for scan_exp() below
263  const ParenMap = (")": "(", "}": "{", "]": "[",);
264  const FwdParenMap = map {$1.value: $1.key}, ParenMap.pairIterator();
265 
266  // private, non-exported constants for parsing
267  const ET_RawString = 0;
268  const ET_QuotedString = 1;
269  const ET_BracketString = 2;
270  const ET_Eq = 3;
271  const ET_Comma = 4;
272  const EtMap = (
273  ET_RawString: "raw string",
274  ET_QuotedString: "quoted string",
275  ET_BracketString: "bracket expression",
276  ET_Eq: "=",
277  ET_Comma: ",",
278  );
279 
280  // private scanner for parse_to_qore_value()
281  list scan_exp(string arg);
282 
283 
284  // private, non-exported function: tries to evaluate the argument as a Qore expression/value
285  hash eval_text(string arg, bool err = False);
286 
287 
288  // private, non-exported function: tries to evaluate the argument as a Qore expression/value
289  /* returns the value parsed
290  */
291  auto parse_to_basic_value(*string arg);
292 
293 
295 
364  auto parse_to_qore_value(string arg);
365 
366 
368 
376  hash<UriQueryInfo> parse_uri_query(string path);
377 
378 
380 
385  string make_uri_query(hash<UriQueryInfo> h, string arg_separator = ";");
386 
387 
389 
405  string get_random_string(softint len = 15, int level = 3);
406 
407 
409 
413  string get_exception_string(hash ex);
414 
415 
417  const StorageNames = ("Kilo", "Mega", "Giga", "Tera", "Peta", "Exa", "Zetta", "Yotta", "Bronto");
418 
420 
428  string get_byte_size(softnumber n, int decimals = 2);
429 
430 
432 
440  string get_marketing_byte_size(softnumber n, int decimals = 2);
441 
442 
444 
455  bool same(list l);
456 
457 
459 
469  list slice(list l, softlist indices);
470 
471 
473 
492  string tmp_location();
493 
494 
496 
513  bool is_int(string text, bool pure = False);
514 
515 
516  const RE_FLOAT = '^\s*[-+]?('
517  '\d+\.\d*(e[-+]?\d+)?' // floating point number, with dot, optional exponent
518  '|\.\d+(e[-+]?\d+)?' // floating point number, starting with a dot, optional exponent
519  '|\d+e[-+]?\d+' // floating point number, without dot, with exponent
520  ')\s*$'; //';
521 
523 
540  bool is_float(string text, bool pure = False);
541 
542 
543  const RE_NUMBER = '^\s*[-+]?('
544  '\d+\.\d*(e[-+]?\d+)?' // floating point number, with dot, optional exponent
545  '|\.\d+(e[-+]?\d+)?' // floating point number, starting with a dot, optional exponent
546  '|\d+e[-+]?\d+' // floating point number, without dot, with exponent
547  '|\d+)n\s*$'; //'; #integer
548 
550 
568  bool is_number(string text, bool pure = False);
569 
570 
572 
587  string lpad(softstring text, int length, string padding = ' ');
588 
589 
591 
606  string rpad(softstring text, int length, string padding = ' ');
607 
608 
610 
621  string ordinal(int i);
622 
623 
625 
644  string plural(int count, string base, string singular = "", string plural = "s");
645 
646 
648 
660  string regex_escape(string text);
661 
662 
664 
676  string glob_to_regex(string pat);
677 
678 
680 
691  list flatten (softlist arg);
692 
693 
695 
710  list uniq (softlist arg);
711 
712 
714 
725  list zip();
726 
727 };
string get_exception_string(hash ex)
returns a multi-line string from the exception hash argument suitable for logging or output on the co...
string normalize_dir_windows(string dir, string cwd=getcwd())
returns a normalized Windows path name (eliminates &quot;.&quot;, &quot;..&quot;, and &quot;//&quot;)
the return value of the parse_uri_query() function
Definition: Util.qm.dox.h:126
string normalize_dir(string dir, string cwd=getcwd())
returns a platform-specific normalized path name (starting from /, eliminates &quot;.&quot;, &quot;..&quot;, and &quot;//&quot;)
bool is_number(string text, bool pure=False)
Checks whether string represents a (true) number value.
string plural(int count, string base, string singular="", string plural="s")
Returns string with number and proper singular/plural form of noun.
string glob_to_regex(string pat)
Translates a shell pattern to a regular expression.
string method
the part of the URI path before the first &quot;?&quot; character or the entire path if no &quot;?&quot; character is present in the path
Definition: Util.qm.dox.h:128
string rpad(softstring text, int length, string padding= ' ')
Returns a string right-padded to a specified length with the specified characters.
*string getcwd()
list zip()
Returns a list of lists, where the i-th list contains the i-th element from each of the argument list...
string get_byte_size(softnumber n, int decimals=2)
returns a string giving a user-friendly real storage size (ie based on 1KiB = 1024 bytes) in KiB...
string normalize_dir_unix(string dir, string cwd=getcwd())
returns a normalized UNIX path name (starting from /, eliminates &quot;.&quot;, &quot;..&quot;, and &quot;//&quot;) ...
string tmp_location()
Returns a path for temporary files.
const False
list list(...)
const StorageNames
storage names (Geop, which would normally follow Bronto, is excluded as the abbreviation would be GB/...
Definition: Util.qm.dox.h:417
string lpad(softstring text, int length, string padding= ' ')
Returns a string left-padded to a specified length with the specified characters. ...
hash< UriQueryInfo > parse_uri_query(string path)
parses a URI path for a arguments and a method; where the method is the part of the path before the f...
bool is_float(string text, bool pure=False)
Checks whether string represents a (true) float value.
string regex_escape(string text)
Escapes (backslashes) all non-alphanumeric characters in a string.
bool same(list l)
checks whether given list has only uniq element
list flatten(softlist arg)
Flattens a nested list (the nesting can be to any depth).
*hash params
(only present if a &quot;?&quot; character is present in the input); the part of the path after the first &quot;...
Definition: Util.qm.dox.h:131
int compare_version(string lv, string rv)
compares complex versions by breaking down strings into component parts
string ordinal(int i)
Returns string with partially textual representation of ordinal integer value.
string get_random_string(softint len=15, int level=3)
function that returns a random string
list uniq(softlist arg)
Returns a duplicate-free version of the list.
list slice(list l, softlist indices)
list slice implementation
auto parse_to_qore_value(string arg)
tries to convert a string (such as an argument given from the command-line) to a Qore value ...
int length(softstring str)
hash hash(object obj)
string make_uri_query(hash< UriQueryInfo > h, string arg_separator=";")
returns a serialized string for a UriQueryInfo hash
string get_marketing_byte_size(softnumber n, int decimals=2)
returns a string giving a user-friendly &quot;marketing storage&quot; size (ie based on 1KB = 1000 bytes) in KB...
bool is_int(string text, bool pure=False)
Checks whether string represents a (true) integer value.