[Home]

Summary:ASTERISK-14227: [patch] New asterisk function REPLACE
Reporter:agjohnson (agjohnson)Labels:
Date Opened:2009-05-29 12:02:29Date Closed:2009-11-23 23:05:43.000-0600
Priority:MajorRegression?No
Status:Closed/CompleteComponents:Functions/func_strings
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:( 0) 20090529__feature15223.diff.txt
( 1) 20090529__feature15223-2.diff.txt
( 2) 20091112__issue15223.diff.txt
( 3) svn-commit.2.tmp
Description:[Synopsis]
Replace a set of characters with another character in a given string


[Description]
Iterates through a string replacing all the <find-chars> with <replace-char>.
NOTE: <replace-char> may be either empty or contain one character

[Syntax]
REPLACE(find-chars,replace-char,string)
Comments:By: agjohnson (agjohnson) 2009-05-29 12:32:43

New synopsis reads:

Replace a set of characters in a given string with another character.

By: Leif Madsen (lmadsen) 2009-09-18 09:02:43

This is marked as Ready for Testing, however, can't this same functionality be performed with a REGEX() ?

By: agjohnson (agjohnson) 2009-09-24 17:01:15

As far as I am aware, the REGEX function in Asterisk just returns a 1 or a 0.

By: Tilghman Lesher (tilghman) 2009-11-11 11:13:38.000-0600

The format of this patch fails review.

1) Lines must be indented with tabs, not spaces.
2) The documentation specifies pipes as delimiters, yet commas are delimiters.
3) In addition, I'd strongly recommend that you use the C function strchr(3) to do your string searches for characters to replace and use ast_get_encoded_str(), to allow nonprintable characters and commas to be specified in the first two fields.

By: Tilghman Lesher (tilghman) 2009-11-12 18:43:20.000-0600

Patch fixed, because I'm impatient and want to get this patch committed.

By: Leif Madsen (lmadsen) 2009-11-18 11:20:40.000-0600

PASSTHRU() works as expected.

   -- Executing [200@phones:2] Set("SIP/0004f2040001-00000000", "count=1") in new stack
   -- Executing [200@phones:3] Set("SIP/0004f2040001-00000000", "myString=this-is-my-custom-string") in new stack
   -- Executing [200@phones:4] Set("SIP/0004f2040001-00000000", "totalFields=5") in new stack
   -- Executing [200@phones:5] NoOp("SIP/0004f2040001-00000000", "--START--") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "1") in new stack
   -- Executing [200@phones:7] NoOp("SIP/0004f2040001-00000000", "this") in new stack
   -- Executing [200@phones:8] Set("SIP/0004f2040001-00000000", "count=2") in new stack
   -- Executing [200@phones:9] EndWhile("SIP/0004f2040001-00000000", "") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "1") in new stack
   -- Executing [200@phones:7] NoOp("SIP/0004f2040001-00000000", "is") in new stack
   -- Executing [200@phones:8] Set("SIP/0004f2040001-00000000", "count=3") in new stack
   -- Executing [200@phones:9] EndWhile("SIP/0004f2040001-00000000", "") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "1") in new stack
   -- Executing [200@phones:7] NoOp("SIP/0004f2040001-00000000", "my") in new stack
   -- Executing [200@phones:8] Set("SIP/0004f2040001-00000000", "count=4") in new stack
   -- Executing [200@phones:9] EndWhile("SIP/0004f2040001-00000000", "") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "1") in new stack
   -- Executing [200@phones:7] NoOp("SIP/0004f2040001-00000000", "custom") in new stack
   -- Executing [200@phones:8] Set("SIP/0004f2040001-00000000", "count=5") in new stack
   -- Executing [200@phones:9] EndWhile("SIP/0004f2040001-00000000", "") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "1") in new stack
   -- Executing [200@phones:7] NoOp("SIP/0004f2040001-00000000", "string") in new stack
   -- Executing [200@phones:8] Set("SIP/0004f2040001-00000000", "count=6") in new stack
   -- Executing [200@phones:9] EndWhile("SIP/0004f2040001-00000000", "") in new stack
   -- Executing [200@phones:6] While("SIP/0004f2040001-00000000", "0") in new stack
   -- Executing [200@phones:10] NoOp("SIP/0004f2040001-00000000", "--ALL DONE--") in new stack






Hmmm... well it works as advertised, but I expected REPLACE() to replace a string with another string.

i.e. I could replace 'abc' with 'ABC'.

Here is the output for REPLACE() in some tests (working as expected)

- Executing [100@phones:2] Set("SIP/0004f2040001-00000000", "originalVariable=123abc789xyzHELLO") in new stack
   -- Executing [100@phones:3] NoOp("SIP/0004f2040001-00000000", "Standard test on numbers: Get '444abc789xyzHELLO'") in new stack
   -- Executing [100@phones:4] NoOp("SIP/0004f2040001-00000000", "Standard test on string: Get '123yyy789xyzHELLO'") in new stack
   -- Executing [100@phones:5] NoOp("SIP/0004f2040001-00000000", "String replace: Get '123abc789xyzhhhhh'") in new stack
   -- Executing [100@phones:6] NoOp("SIP/0004f2040001-00000000", "Case match: Get '123abc789xyzHELLO'") in new stack
   -- Executing [100@phones:7] NoOp("SIP/0004f2040001-00000000", "Case replace: Get '123Abc789xyzHELLO'") in new stack

By: Leif Madsen (lmadsen) 2009-11-18 11:20:53.000-0600

Testing dialplan for above output:

exten => 100,1,NoOp()
exten => 100,n,Set(originalVariable=123abc789xyzHELLO)
exten => 100,n,NoOp(Standard test on numbers: Get '${REPLACE(originalVariable,123,4)}')
exten => 100,n,NoOp(Standard test on string: Get '${REPLACE(originalVariable,abc,y)}')
exten => 100,n,NoOp(String replace: Get '${REPLACE(originalVariable,HELLO,hello)}')
exten => 100,n,NoOp(Case match: Get '${REPLACE(originalVariable,XYZ,x)}')
exten => 100,n,NoOp(Case replace: Get '${REPLACE(originalVariable,a,A)}')

exten => 200,1,NoOp()
exten => 200,n,Set(count=1)
exten => 200,n,Set(myString=this-is-my-custom-string)
exten => 200,n,Set(totalFields=${FIELDQTY(PASSTHRU(this^is^my^custom^string),^)})
exten => 200,n,NoOp(--START--)
exten => 200,n,While($[${count} <= ${totalFields}])
exten => 200,n,NoOp(${CUT(PASSTHRU(${myString}),-,${count})})
exten => 200,n,Set(count=$[${count} + 1])
exten => 200,n,EndWhile()
exten => 200,n,NoOp(--ALL DONE--)

By: Digium Subversion (svnbot) 2009-11-23 23:05:42.000-0600

Repository: asterisk
Revision: 230994

U   trunk/CHANGES
U   trunk/funcs/func_strings.c
U   trunk/include/asterisk/app.h

------------------------------------------------------------------------
r230994 | tilghman | 2009-11-23 23:05:41 -0600 (Mon, 23 Nov 2009) | 16 lines

Add REPLACE & PASSTHRU functions, overhaul of func_strings, fix API docs for the ast_get_encoded_* functions.
* Add REPLACE function, which searches a given variable for a set of
  characters and replaces each with a given character.
* Add PASSTHRU function, which passes a literal string back, like a NoOp for
  functions.  Intent is to be able to specify a literal string to another
  function that takes a variable name as an argument.
* Let the array manipulation functions work with dialplan functions, in
  addition to variables.  This allows the array manipulation functions to
  modify ASTDB and ODBC backends, assuming the func_odbc configuration has
  both read and write functions.
(closes issue ASTERISK-14227)
Reported by: ajohnson
Patches:
      20091112__issue15223.diff.txt uploaded by tilghman (license 14)
Tested by: lmadsen, tilghman

------------------------------------------------------------------------

http://svn.digium.com/view/asterisk?view=rev&revision=230994