ref: 09ca67c40a822dc77a3049dc1bc35565b3081c9f
parent: 4a7d019d03fa6c267d27ffb9d90e2472bad4bcbf
author: Ori Bernstein <[email protected]>
date: Wed Dec 11 19:20:39 EST 2013
Add string strip functions.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -11,8 +11,8 @@
fmt.myr \
hashfuncs.myr \
htab.myr \
- ipparse.myr \
intparse.myr \
+ ipparse.myr \
now.myr \
option.myr \
optparse.myr \
@@ -23,7 +23,10 @@
sldup.myr \
sleq.myr \
sljoin.myr \
+ slpush.myr \
slurp.myr \
+ strsplit.myr \
+ strstrip.myr \
strcmp.myr \
sys.myr \
types.myr \
--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -118,7 +118,7 @@
/* if the slice wouldn't change buckets, we don't need to realloc. */
if samebucket(sl.len * sizeof(@a), len * sizeof(@a))
- -> sl
+ -> (sl castto(@a#))[:len]
;;
new = slalloc(len)
--- a/libstd/resolve.myr
+++ b/libstd/resolve.myr
@@ -3,7 +3,10 @@
use "endian.use"
use "error.use"
use "fmt.use"
+use "option.use"
use "slcp.use"
+use "slurp.use"
+use "strsplit.use"
use "sys.use"
use "types.use"
@@ -37,9 +40,33 @@
const resolve : (host : byte[:] -> error(hostinfo[:], resolveerr))
;;
+const Hostfile = "/etc/hosts"
+
const resolve = {host : byte[:]
- /* FIXME: read /etc/hosts */
- -> dnsresolve(host)
+ match hostfind(host)
+ | `Some h: -> h
+ | `None: -> dnsresolve(host)
+ ;;
+}
+
+const hostfind = {host
+ var hdat
+ var lines
+ /*
+ var ip
+ var host
+ */
+ var i
+
+ match slurp(Hostfile)
+ | `Success h: hdat = h
+ | `Failure m: -> `None
+ ;;
+
+ lines = strsplit(hdat, "\n")
+ for i = 0; i < lines.len; i++
+ put("Hosts: Line %s\n", lines[i])
+ ;;
}
const dnsresolve = {host : byte[:]
--- /dev/null
+++ b/libstd/strsplit.myr
@@ -1,0 +1,29 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "fmt.use"
+use "slpush.use"
+use "sys.use"
+use "types.use"
+
+pkg std =
+ const strsplit : (s : byte[:], delim : byte[:] -> byte[:][:])
+;;
+
+const strsplit = {s, delim
+ var i
+ var last
+ var sp
+
+ sp = [][:]
+ last = 0
+ assert(delim.len == 1, "FIXME: We should support strings as delimiters")
+ for i = 0; i < s.len; i++
+ if s[i] == delim[0]
+ sp = slpush(sp, s[last:i])
+ last = i + 1
+ ;;
+ ;;
+ -> sp
+}
+
--- /dev/null
+++ b/libstd/strstrip.myr
@@ -1,0 +1,41 @@
+use "types.use"
+use "utf.use"
+use "chartype.use"
+
+pkg std =
+ const strstrip : (str : byte[:] -> byte[:])
+ const strfstrip : (str : byte[:] -> byte[:])
+ const strrstrip : (str : byte[:] -> byte[:])
+;;
+
+const strstrip = {str
+ -> strrstrip(strfstrip(str))
+}
+
+const strfstrip = {str
+ var c
+
+ for c = decode(str); isblank(c); c = decode(str)
+ str = str[charlen(c):]
+ ;;
+ -> str
+
+}
+
+const strrstrip = {str
+ var i
+ var end
+
+ /* scan backwards for start of utf char */
+ end = 0
+ for i = str.len; i != 0; i--
+ if str[i] & 0x80 == 0
+ if !isspace(decode(str[i-1:]))
+ goto donestrip
+ ;;
+ end = i - 1
+ ;;
+ ;;
+:donestrip
+ -> str[:end]
+}
--- a/mi/df.c
+++ b/mi/df.c
@@ -13,7 +13,28 @@
#include "parse.h"
#include "opt.h"
+/*
+static void nodeuse(Node *n, Bitset *bs)
+{
+}
+
+static void nodedef(Node *n, Bitset *bs)
+{
+}
+
+static void bbuse(Bb *bb, Bitset *bs)
+{
+}
+
+static void bbdef(Bb *bb, Bitset *bs)
+{
+}
+*/
+
void flow(Cfg *cfg)
{
}
+void checkret(Cfg *cfg)
+{
+}
--- a/mi/opt.h
+++ b/mi/opt.h
@@ -3,6 +3,8 @@
struct Cfg {
Bb **bb;
+ Bb *start;
+ Bb *end;
size_t nbb;
/* for building bb */