[Asterisk-Dev] Patch to add relative lengths to SubString

Reini Urban r.urban at inode.at
Tue Jun 10 08:44:11 MST 2003

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 
  "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 
+"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

More information about the asterisk-dev mailing list