string pad/rpad/lpad
This commit is contained in:
parent
08b5053829
commit
46b29fc229
|
|
@ -46,6 +46,11 @@
|
|||
#### Performance
|
||||
- push_back - repeatedly without reserving size
|
||||
|
||||
#### Install
|
||||
```
|
||||
cp build/ml /usr/local/bin/ml
|
||||
cp stdlib/stdlib.lsp /usr/local/var/mlisp/stdlib.lsp
|
||||
```
|
||||
|
||||
|
||||
### Example of use
|
||||
|
|
|
|||
27
debug.lsp
27
debug.lsp
|
|
@ -18,31 +18,10 @@
|
|||
;; (print (member '(1 2 3) 10))
|
||||
|
||||
|
||||
(define csv_list '())
|
||||
(for f (ls-dir "tests/divi")
|
||||
(if (regex-search? f "^divi.*\.csv$")
|
||||
(do
|
||||
(define filename (+ "tests/divi/" f))
|
||||
; (print filename)
|
||||
(define csv_str (read-file filename))
|
||||
(define csv_file_list (parse-csv csv_str))
|
||||
(define csv_list (+ csv_list csv_file_list))
|
||||
)
|
||||
))
|
||||
|
||||
;; (for x csv_list (print x))
|
||||
(print (string-rpad "rpad0123456789" 10 "x"))
|
||||
(print (string-rpad "rpad " 10 "x"))
|
||||
|
||||
(print (len csv_list))
|
||||
|
||||
(define my_tickers '("FDX" "C" "AIG" "BAC" "BK" "PBF" "PBFX" "SYF" "WFC" "TEVA" "XOM"))
|
||||
|
||||
; (print (filter (lambda (x) (= (first x) "CLX")) csv_list))
|
||||
; (print (filter (lambda (x) (member my_tickers (first x))) csv_list))
|
||||
(define csv_list (filter (lambda (x) (member my_tickers (first x))) csv_list))
|
||||
|
||||
|
||||
(define sorted_list (quick-sort-by csv_list (lambda (a b) (> (str-to-date (second a) "%m/%d/%Y") (str-to-date (second b) "%m/%d/%Y")))))
|
||||
|
||||
(for x csv_list (print x))
|
||||
(print (string-lpad "lpad" 10 "x"))
|
||||
|
||||
(print "Debug ends")
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@
|
|||
|`(display ..)`|||
|
||||
|`(replace ..)`|||
|
||||
|`(regex-search? ..)`|||
|
||||
|`(string-pad str len char rpad_lpad)`|||
|
||||
|`(int ..)`|||
|
||||
|`(float ..)`|||
|
||||
|`(eval ..)`|||
|
||||
|
|
|
|||
13
ml.cpp
13
ml.cpp
|
|
@ -1496,11 +1496,21 @@ namespace builtin {
|
|||
eval_args(args, env);
|
||||
|
||||
if (args.size() != 2) // if (args.size() < 2 || args.size() > 3)
|
||||
throw MlError(MlValue("regex_search", regex_search), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
throw MlError(MlValue("regex_search", regex_search), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
|
||||
return MlValue(regexp_search(args[0].as_string(), args[1].as_string()));
|
||||
}
|
||||
|
||||
MlValue string_pad(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
eval_args(args, env);
|
||||
|
||||
if (args.size() != 4)
|
||||
throw MlError(MlValue("regex_search", regex_search), env, args.size() > 4 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
|
||||
// TODO validate len > 0 etc
|
||||
return MlValue::string(string_padd(args[0].as_string(), args[1].as_int(), args[2].as_string()[0], (args[3].as_string()=="rpad")));
|
||||
}
|
||||
|
||||
MlValue display(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
eval_args(args, env);
|
||||
|
||||
|
|
@ -1724,6 +1734,7 @@ MlValue MlEnvironment::get(const std::string &name) const {
|
|||
if (name == "display") return MlValue("display", builtin::display);
|
||||
if (name == "replace") return MlValue("replace", builtin::replace);
|
||||
if (name == "regex-search?") return MlValue("regex-search?", builtin::regex_search);
|
||||
if (name == "string-pad") return MlValue("string-pad", builtin::string_pad);
|
||||
|
||||
// Casting operations
|
||||
if (name == "int") return MlValue("int", builtin::cast_to_int);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
#include "ml_string.h"
|
||||
|
||||
|
||||
|
||||
// Replace a substring with a replacement string in a source string
|
||||
void replace_substring(std::string &src, const std::string &substr, const std::string &replacement) {
|
||||
size_t i = 0;
|
||||
|
|
@ -30,3 +29,18 @@ bool regexp_search(const std::string &where, const std::string ®ex_str) {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string string_padd(const std::string &str, int pad_len, char fill_char, bool from_right) {
|
||||
int str_len = str.length();
|
||||
|
||||
if (str_len == pad_len)
|
||||
return str;
|
||||
else if (str_len > pad_len) {
|
||||
return (from_right ? str.substr(0, pad_len) : str.substr(str_len - pad_len, std::string::npos));
|
||||
}
|
||||
|
||||
if (from_right)
|
||||
return str + std::string(pad_len - str.size(), fill_char);
|
||||
else
|
||||
return std::string(pad_len - str.size(), fill_char) + str;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@
|
|||
#include <string>
|
||||
#include <regex>
|
||||
|
||||
|
||||
// Replace a substring with a replacement string in a source string
|
||||
void replace_substring(std::string &src, const std::string &substr, const std::string &replacement);
|
||||
|
||||
// Returns true if where contains regex
|
||||
bool regexp_search(const std::string &where, const std::string ®ex_str);
|
||||
|
||||
std::string string_padd(const std::string & str, int pad_len, char fill_char, bool from_right);
|
||||
|
|
@ -26,6 +26,16 @@
|
|||
(defun inc (n) (+ n 1))
|
||||
|
||||
|
||||
; pad string on the end
|
||||
(defun string-rpad (str length pad_char)
|
||||
(string-pad str length pad_char "rpad"))
|
||||
|
||||
; pad string on the begining
|
||||
(defun string-lpad (str length pad_char)
|
||||
(string-pad str length pad_char "lpad"))
|
||||
|
||||
|
||||
|
||||
|
||||
; return second element of list
|
||||
(defun second (l) (index l 1))
|
||||
|
|
|
|||
Loading…
Reference in New Issue