From 8515d3aa57254c95a1afad4fc64324b1f2a2755f Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 4 Dec 2013 15:11:48 -0600 Subject: [PATCH] Support omission of string slice low/high indices, BIT-1097. Omission of the low index defaults to 0: s = "12345"; s[:3] == "123" Omission of the high index defaults to length of the string: s = "12345"; s[3:] == "45" --- src/parse.y | 17 ++++++--- .../Baseline/language.string-indexing/out | 35 +++++++++++++++++++ testing/btest/language/string-indexing.bro | 8 +++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/parse.y b/src/parse.y index 98df0de2a3..d5ea63e5a8 100644 --- a/src/parse.y +++ b/src/parse.y @@ -55,7 +55,7 @@ %type opt_init %type TOK_CONSTANT %type pattern -%type expr init anonymous_function +%type expr opt_expr init anonymous_function %type event %type stmt stmt_list func_body for_head %type type opt_type enum_body @@ -261,6 +261,13 @@ decl_list: | ; +opt_expr: + expr + { $$ = $1; } + | + { $$ = 0; } + ; + expr: '(' expr ')' { @@ -418,11 +425,13 @@ expr: $$ = new IndexExpr($1, $3); } - | expr '[' expr ':' expr ']' + | expr '[' opt_expr ':' opt_expr ']' { set_location(@1, @6); - ListExpr* le = new ListExpr($3); - le->Append($5); + Expr* low = $3 ? $3 : new ConstExpr(new Val(0, TYPE_COUNT)); + Expr* high = $5 ? $5 : new SizeExpr($1); + ListExpr* le = new ListExpr(low); + le->Append(high); $$ = new IndexExpr($1, le, true); } diff --git a/testing/btest/Baseline/language.string-indexing/out b/testing/btest/Baseline/language.string-indexing/out index 99464302ed..4fe25c8161 100644 --- a/testing/btest/Baseline/language.string-indexing/out +++ b/testing/btest/Baseline/language.string-indexing/out @@ -28,6 +28,41 @@ word[5] = word[6] = word[7] = word[100] = +word[:-100] = +word[:-7] = +word[:-6] = +word[:-5] = +word[:-4] = H +word[:-3] = He +word[:-2] = Hel +word[:-1] = Help +word[:0] = +word[:1] = H +word[:2] = He +word[:3] = Hel +word[:4] = Help +word[:5] = HelpA +word[:6] = HelpA +word[:7] = HelpA +word[:100] = HelpA +word[-100:] = HelpA +word[-7:] = HelpA +word[-6:] = HelpA +word[-5:] = HelpA +word[-4:] = elpA +word[-3:] = lpA +word[-2:] = pA +word[-1:] = A +word[0:] = HelpA +word[1:] = elpA +word[2:] = lpA +word[3:] = pA +word[4:] = A +word[5:] = +word[6:] = +word[7:] = +word[100:] = +HelpA A 1234 diff --git a/testing/btest/language/string-indexing.bro b/testing/btest/language/string-indexing.bro index bff37f6bec..e109eeba80 100644 --- a/testing/btest/language/string-indexing.bro +++ b/testing/btest/language/string-indexing.bro @@ -24,6 +24,14 @@ s = "012345"; for ( i in indices ) print fmt("word[%s] = %s", indices[i], word[indices[i]]); +for ( i in indices ) + print fmt("word[:%s] = %s", indices[i], word[:indices[i]]); + +for ( i in indices ) + print fmt("word[%s:] = %s", indices[i], word[indices[i]:]); + +print word[:]; + print ""; print "A";