mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: regex helper functions are done
This commit is contained in:
parent
5c736fd224
commit
dac3bad33a
@ -7,4 +7,12 @@
|
||||
(register global-match (Fn [&Pattern &String] (Array (Array String))))
|
||||
(register substitute (Fn [&Pattern &String &String Int] String))
|
||||
(defn matches? [pat s] (/= (find pat s) -1))
|
||||
|
||||
(register str (Fn [&Pattern] String))
|
||||
(register prn (Fn [&Pattern] String))
|
||||
|
||||
(register init (Fn [&String] Pattern))
|
||||
(register = (Fn [&Pattern &Pattern] Bool))
|
||||
(register delete (Fn [Pattern] ()))
|
||||
(register copy (Fn [&Pattern] Pattern))
|
||||
)
|
||||
|
@ -482,7 +482,7 @@ string Pattern_match_MINUS_str(string* p, string* s) {
|
||||
int start = (s1 - str) + 1;
|
||||
int end = res - str + 1;
|
||||
int len = end - start;
|
||||
res = malloc(len + 1);
|
||||
res = CARP_MALLOC(len + 1);
|
||||
memcpy(res, s1, len);
|
||||
res[len] = '\0';
|
||||
return res;
|
||||
@ -614,3 +614,37 @@ string Pattern_substitute(string* p, string *s, string *t, int ns) {
|
||||
snprintf(res, l, "%s%s", res, str);
|
||||
return res;
|
||||
}
|
||||
|
||||
string Pattern_copy(string *p) {
|
||||
size_t len = strlen(*p) + 1;
|
||||
string ptr = CARP_MALLOC(len);
|
||||
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (string) memcpy(ptr, *p, len);
|
||||
}
|
||||
|
||||
void Pattern_delete(string p) {
|
||||
CARP_FREE(p);
|
||||
}
|
||||
|
||||
string Pattern_init(string* p) {
|
||||
return Pattern_copy(p);
|
||||
}
|
||||
|
||||
string Pattern_str(string *p) {
|
||||
return Pattern_copy(p);
|
||||
}
|
||||
|
||||
string Pattern_prn(string *p) {
|
||||
int n = strlen(*p) + 4;
|
||||
string buffer = CARP_MALLOC(n);
|
||||
snprintf(buffer, n, "#\"%s\"", *p);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool Pattern__EQ_(string *a, string *b) {
|
||||
return strcmp(*a, *b) == 0;
|
||||
}
|
||||
|
@ -179,7 +179,8 @@ isManaged typeEnv (StructTy name _) =
|
||||
Just (_, Binder (XObj wrong _ _)) -> error ("Invalid XObj in type env: " ++ show wrong)
|
||||
Nothing -> error ("Can't find " ++ name ++ " in type env.")
|
||||
)
|
||||
isManaged _ StringTy = True
|
||||
isManaged _ StringTy = True
|
||||
isManaged _ PatternTy = True
|
||||
isManaged _ _ = False
|
||||
|
||||
{-# ANN validateMembers "HLint: ignore Eta reduce" #-}
|
||||
@ -210,13 +211,14 @@ okXObjForType typeEnv typeVariables xobj =
|
||||
canBeUsedAsMemberType :: TypeEnv -> [Ty] -> Ty -> Either String ()
|
||||
canBeUsedAsMemberType typeEnv typeVariables t =
|
||||
case t of
|
||||
IntTy -> return ()
|
||||
FloatTy -> return ()
|
||||
DoubleTy -> return ()
|
||||
LongTy -> return ()
|
||||
BoolTy -> return ()
|
||||
StringTy -> return ()
|
||||
CharTy -> return ()
|
||||
IntTy -> return ()
|
||||
FloatTy -> return ()
|
||||
DoubleTy -> return ()
|
||||
LongTy -> return ()
|
||||
BoolTy -> return ()
|
||||
StringTy -> return ()
|
||||
PatternTy -> return ()
|
||||
CharTy -> return ()
|
||||
PointerTy inner -> do _ <- canBeUsedAsMemberType typeEnv typeVariables inner
|
||||
return ()
|
||||
StructTy "Array" [inner] -> do _ <- canBeUsedAsMemberType typeEnv typeVariables inner
|
||||
|
@ -4,6 +4,31 @@
|
||||
|
||||
(defn main []
|
||||
(with-test test
|
||||
(assert-equal test
|
||||
#"1234"
|
||||
&(init "1234")
|
||||
"init works as expected"
|
||||
)
|
||||
(assert-equal test
|
||||
"1234"
|
||||
&(str #"1234")
|
||||
"str works as expected"
|
||||
)
|
||||
(assert-equal test
|
||||
"#\"1234\""
|
||||
&(prn #"1234")
|
||||
"prn works as expected"
|
||||
)
|
||||
(assert-equal test
|
||||
true
|
||||
(= #"1234" #"1234")
|
||||
"= works as expected on equality"
|
||||
)
|
||||
(assert-equal test
|
||||
false
|
||||
(= #"1234" #"235")
|
||||
"= works as expected on non-equality"
|
||||
)
|
||||
(assert-equal test
|
||||
3
|
||||
(find #"\d" " 12")
|
||||
|
Loading…
Reference in New Issue
Block a user