Summary:ASTERISK-00226: [patch] Add relative substring length to SubString
Reporter:John Todd (jtodd)Labels:
Date Opened:2003-09-05 19:27:03Date Closed:2008-01-15 14:35:12.000-0600
Versions:Frequency of
Environment:Attachments:( 0) substring.diff
I have found that this is a pretty handy feature, but I don't see it in the main CVS distribution.  Fairly minor.  I don't know if Reini has submitted a disclaimer, but he put in a number of patches a while back so I believe he has.

From: Reini Urban <r.urban@inode.at>
Organization: inode.at
To: asterisk-dev@lists.digium.com
Subject: [Asterisk-Dev] Patch to add relative lengths to SubString
Reply-To: asterisk-dev@lists.digium.com
Date: Tue, 10 Jun 2003 17:44:11 +0200

The following patch adds relative lengths to SubString, as with the PHP or Perl substr() function. The 2nd param may be negative.

This should be extended to normal string assignment also:
e.g. ${EXTEN:0:-4} gets a SIP number without the 4-digit extension.

sh-2.05a# diff -bu apps/app_substring.c~ apps/app_substring.c
--- apps/app_substring.c~       2003-06-10 17:42:01.000000000 +0200
+++ apps/app_substring.c        2003-06-10 17:42:01.000000000 +0200
@@ -33,13 +33,16 @@
"of string_of_digits to a given variable. Parameter count1 may be positive\n"
"or negative. If it's positive then we skip the first count1 digits from the\n"
"left. If it's negative, we move count1 digits counting from the end of\n"
-"the string to the left. Parameter count2 may be only positive and implies\n"
+"the string to the left. Parameter count2 implies\n"
"how many digits we're taking from the point that count1 placed us.\n"
+"If count2 is negative then that many digits are omitted from the end.\n"
"For example:\n"
"exten => _NXXXXXX,1,SubString,test=2564286161|0|3\n"
"assigns the area code (3 first digits) to variable test.\n"
"exten => _NXXXXXX,1,SubString,test=2564286161|-7|7\n"
"assigns the last 7 digits to variable test.\n"
+"exten => _NXXXXXX,1,SubString,test=2564286161|0|-4\n"
+"assigns all but the last 4 digits to variable test.\n"
"If there are no parameters it'll return with -1.\n"
"If there wrong parameters it go on and return with 0\n";

@@ -71,9 +74,8 @@
-    if (icount2<=0) {
-      ast_log(LOG_DEBUG, "Exiting, wrong parameter count2\n");
-      return -1;
+    if (icount2<0) {
+      icount2 = icount2 + strlen(second);
    if (abs(icount1)>strlen(second)) {
      ast_log(LOG_WARNING, "Limiting count1 parameter because it exceeds the length of the string\n");

Reini Urban - Entwicklung - http://inode.at
Comments:By: Mark Spencer (markster) 2003-09-08 14:43:13

This is very minor.  Can you attach the patch here, but not paste it in a bugnote?

By: Mark Spencer (markster) 2003-09-26 21:38:01

Fixed in CVS

By: Digium Subversion (svnbot) 2008-01-15 14:35:12.000-0600

Repository: asterisk
Revision: 1569

U   trunk/apps/app_substring.c

r1569 | markster | 2008-01-15 14:35:12 -0600 (Tue, 15 Jan 2008) | 2 lines

Add relative substring (bug ASTERISK-226)