1
0
Fork 0

string.nas: new functions string.join() and string.replace()

debug.nas: let benchmark() repeat tested function
io.nas: cosmetics
aircraft.nas: initialize time property only once
This commit is contained in:
mfranz 2008-06-21 07:10:32 +00:00
parent 84044b4603
commit d86b1b9c74
4 changed files with 41 additions and 20 deletions

View file

@ -328,7 +328,7 @@ var lowpass = {
me.value = v;
},
_filter_ : func(v) {
var dt = getprop("/sim/time/delta-sec") or 0;
var dt = getprop("/sim/time/delta-sec");
var c = dt / (me.coeff + dt);
me.value = v * c + me.value * (1 - c);
},
@ -954,8 +954,9 @@ var HUD = {
#
_setlistener("/sim/signals/nasal-dir-initialized", func {
props.initNode("/sim/time/delta-sec", 0);
props.initNode("/sim/time/delta-realtime-sec", 0.00000001);
props.globals.getNode("/sim/time/delta-realtime-sec", 1).setDoubleValue(0.00000001);
HUD.init();
data.init();
autotrim.init();

View file

@ -32,9 +32,9 @@
# <verb>ose is by default 1, and suppressed the
# node's refcounter if 0.
#
# debug.benchmark(<label:string>, <func> [<args>])
# ... calls function with optional args and
# prints execution time in seconds,
# debug.benchmark(<label:string>, <func> [, <repeat:int>])
# ... runs function <repeat> times (default: 1)
# and prints execution time in seconds,
# prefixed with <label>.
#
# debug.printerror(<err-vector>) ... prints error vector as set by call()
@ -297,16 +297,17 @@ var proptrace = func(root = "/", frames = 1) {
##
# Executes function f with optional arguments and prints execution
# Executes function fun with repeat times prints execution
# time in seconds. Examples:
#
# var test = func(n) { for (var i = 0; i < n; i +=1) { print(i) }
# debug.benchmark("test()/1", test, 10);
# debug.benchmark("test()/2", func { test(10) });
# var test = func { getprop("/sim/aircraft"); }
# debug.benchmark("test()/2", 1000, test);
# debug.benchmark("test()/2", 1000, func setprop("/sim/aircraft", ""));
#
var benchmark = func(label, f, arg...) {
var benchmark = func(label, fun, repeat = 1) {
var start = systime();
call(f, arg);
for (var i = 0; i < repeat; i += 1)
fun();
print(_bench(sprintf(" %s --> %.6f s ", label, systime() - start)));
}

View file

@ -160,17 +160,15 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
read_rules = [];
write_rules = [];
var file = open(path, "r");
var no = 0;
while ((var line = readln(file)) != nil) {
no += 1;
for(var no = 1; (var line = readln(file)) != nil; no += 1) {
if(!size(line) or line[0] == `#`)
continue;
var f = split(" ", line);
if(size(f) < 3 or (f[0] != "READ" and f[0] != "WRITE") or (f[1] != "DENY" and f[1] != "ALLOW")) {
if(size(f) < 3 or f[0] != "READ" and f[0] != "WRITE" or f[1] != "DENY" and f[1] != "ALLOW") {
printlog("alert", "ERROR: invalid io.open() rule in ", path, ", line ", no, ": ", line);
read_rules = write_rules = [];
break; # don't use die() or return, as io.open() has yet to be redefined
break; # don't use die(), as io.open() has yet to be redefined
}
var pattern = f[2];
foreach(var p; subvec(f, 3))

View file

@ -36,6 +36,27 @@ var trim = func(s, lr = 0) {
}
##
# Join all elements of a list inserting a separator between every two of them.
#
var join = func(sep, list) {
if (!size(list))
return "";
var str = list[0];
foreach (var s; subvec(list, 1))
str ~= sep ~ s;
return str;
}
##
# Replace all occurrences of 'old' by 'new'.
#
var replace = func(str, old, new) {
join(new, split(old, str));
}
##
# return string converted to lower case letters
#
@ -180,7 +201,7 @@ var fixpath = func(path) {
}
if (!size(stack))
return "/";
path = stack[0];
var path = stack[0];
foreach (var s; subvec(stack, 1))
path ~= "/" ~ s;
return prefix ~ path;