Qore Util Module Reference  1.3
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 
114 namespace Init {
115  init();
116 
117 };
118 
120 namespace Util {
122 
124  public struct UriQueryInfo {
126  string method;
127 
130  };
131 
133 
146  int compare_version(string lv, string rv);
147 
148 
150 
168  string normalize_dir(string dir, string cwd = getcwd());
169 
170 
172 
187  string normalize_dir_unix(string dir, string cwd = getcwd());
188 
189 
191 
206  string normalize_dir_windows(string dir, string cwd = getcwd());
207 
208 
210 
223  bool absolute_path(string path);
224 
225 
227 
240  bool absolute_path_unix(string path);
241 
242 
244 
257  bool absolute_path_windows(string path);
258 
259 
260  // private, non-exported constant hashes for scan_exp() below
261  const ParenMap = (")": "(", "}": "{", "]": "[",);
262  const FwdParenMap = map {$1.value: $1.key}, ParenMap.pairIterator();
263 
264  // private, non-exported constants for parsing
265  const ET_RawString = 0;
266  const ET_QuotedString = 1;
267  const ET_BracketString = 2;
268  const ET_Eq = 3;
269  const ET_Comma = 4;
270  const EtMap = (
271  ET_RawString: "raw string",
272  ET_QuotedString: "quoted string",
273  ET_BracketString: "bracket expression",
274  ET_Eq: "=",
275  ET_Comma: ",",
276  );
277 
278  // private scanner for parse_to_qore_value()
279  list scan_exp(string arg);
280 
281 
282  // private, non-exported function: tries to evaluate the argument as a Qore expression/value
283  hash eval_text(string arg, bool err = False);
284 
285 
286  // private, non-exported function: tries to evaluate the argument as a Qore expression/value
287  /* returns the value parsed
288  */
289  auto parse_to_basic_value(*string arg);
290 
291 
293 
362  auto parse_to_qore_value(string arg);
363 
364 
366 
374  hash<UriQueryInfo> parse_uri_query(string path);
375 
376 
378 
383  string make_uri_query(hash<UriQueryInfo> h, string arg_separator = ";");
384 
385 
387 
403  string get_random_string(softint len = 15, int level = 3);
404 
405 
407 
411  string get_exception_string(hash ex);
412 
413 
415  const StorageNames = ("Kilo", "Mega", "Giga", "Tera", "Peta", "Exa", "Zetta", "Yotta", "Bronto");
416 
418 
426  string get_byte_size(softnumber n, int decimals = 2);
427 
428 
430 
438  string get_marketing_byte_size(softnumber n, int decimals = 2);
439 
440 
442 
453  bool same(list l);
454 
455 
457 
467  list slice(list l, softlist indices);
468 
469 
471 
490  string tmp_location();
491 
492 
494 
511  bool is_int(string text, bool pure = False);
512 
513 
514  const RE_FLOAT = '^\s*[-+]?('
515  '\d+\.\d*(e[-+]?\d+)?' // floating point number, with dot, optional exponent
516  '|\.\d+(e[-+]?\d+)?' // floating point number, starting with a dot, optional exponent
517  '|\d+e[-+]?\d+' // floating point number, without dot, with exponent
518  ')\s*$'; //';
519 
521 
538  bool is_float(string text, bool pure = False);
539 
540 
541  const RE_NUMBER = '^\s*[-+]?('
542  '\d+\.\d*(e[-+]?\d+)?' // floating point number, with dot, optional exponent
543  '|\.\d+(e[-+]?\d+)?' // floating point number, starting with a dot, optional exponent
544  '|\d+e[-+]?\d+' // floating point number, without dot, with exponent
545  '|\d+)n\s*$'; //'; #integer
546 
548 
566  bool is_number(string text, bool pure = False);
567 
568 
570 
585  string lpad(softstring text, int length, string padding = ' ');
586 
587 
589 
604  string rpad(softstring text, int length, string padding = ' ');
605 
606 
608 
619  string ordinal(int i);
620 
621 
623 
642  string plural(int count, string base, string singular = "", string plural = "s");
643 
644 
646 
658  string regex_escape(string text);
659 
660 
662 
674  string glob_to_regex(string pat);
675 
676 
678 
689  list flatten (softlist arg);
690 
691 
693 
704  list zip();
705 
706 };
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 ".", "..", and "//")
string rpad(softstring text, int length, string padding=' ')
Returns a string right-padded to a specified length with the specified characters.
the return value of the parse_uri_query() function
Definition: Util.qm.dox.h:124
string normalize_dir(string dir, string cwd=getcwd())
returns a platform-specific normalized path name (starting from /, eliminates ".", "..", and "//")
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 "?" character or the entire path if no "?" character is present in the path
Definition: Util.qm.dox.h:126
*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 lpad(softstring text, int length, string padding=' ')
Returns a string left-padded to a specified length with the specified characters. ...
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 ".", "..", and "//") ...
string tmp_location()
Returns a path for temporary files.
const False
bool absolute_path_unix(string path)
returns True if the argument is a UNIX absolute path, False if not
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:415
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.
the Util namespace contains all the objects in the Util module
Definition: Util.qm.dox.h:120
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 "?" character is present in the input); the part of the path after the first "...
Definition: Util.qm.dox.h:129
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
Definition: Util.qm.dox.h:114
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)
bool absolute_path_windows(string path)
returns True if the argument is a Windows absolute path, False if not
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 "marketing storage" 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.
bool absolute_path(string path)
returns True if the argument is an absolute path, False if not