[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Regexp: one or two functions? (KSpread)
From:       Tomas Mecir <mecirt () gmail ! com>
Date:       2004-08-17 8:50:15
Message-ID: 492258b1040817015023e42fd4 () mail ! gmail ! com
[Download RAW message or body]

Greetings!

I'm facing a dilemma... I have this patch adding support for regexp
search and replace functions to KSpread, originally I wrote 'em as two
separate functions, but then Ariya said, it should be one function,
because that would less confuse users, so I made it one function, but
now I'm in doubt, for that one function seems to be more confusing
than two functions, hence I'd like to hear others' opinion, on whether
there should be two functions (regexp search / replace), or one
do-it-all function. Man, now that's a LONG sentence ;) Patches adding
it in both ways attached.

/ Tomas

["patch.regexp.funs" (application/octet-stream)]
["patch.regexp.funs.2" (text/x-troff-man)]

Index: kspread_functions_text.cc
===================================================================
RCS file: /home/kde/koffice/kspread/kspread_functions_text.cc,v
retrieving revision 1.25
diff -u -3 -p -r1.25 kspread_functions_text.cc
--- kspread_functions_text.cc	24 Jul 2004 13:14:28 -0000	1.25
+++ kspread_functions_text.cc	14 Aug 2004 18:25:48 -0000
@@ -544,35 +544,70 @@ bool kspreadfunc_regexp(KSContext & cont
 {
   QValueList<KSValue::Ptr>& args = context.value()->listValue();
 
-  if (args.count() != 3)
+  if (args.count() < 2)
     return false;
 
   if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
     return false;
   if ( !KSUtil::checkType( context, args[1], KSValue::StringType, true ) )
     return false;
-  if ( !KSUtil::checkType( context, args[2], KSValue::StringType, true ) )
-    return false;
 
-  kdDebug() << "Got parameter" << endl;
+  //do we want to replace?
+  bool isReplace = false;
+  if (args.count() > 2)    
+  {
+    if ( !KSUtil::checkType( context, args[2], KSValue::BoolType, true ) )
+      return false;
+    isReplace = args[2]->boolValue ();
+  }
+  
+  QString s (args[0]->stringValue());
 
-  QRegExp exp( args[1]->stringValue() );
+  QRegExp exp (args[1]->stringValue());
   //  exp.setWildcard( true );
   if ( !exp.isValid() )
     return false;
 
-  QString s( args[0]->stringValue() );
-  QString str( args[2]->stringValue() );
+  //two modes of operation - find or replace
+  if (isReplace)
+  {
+    if (args.count() != 4)
+      return false;
+    if ( !KSUtil::checkType( context, args[3], KSValue::StringType, true ) )
+      return false;
 
-  kdDebug() << "Search: " << args[1]->stringValue() << " in " << s
-            << ", Result: " << exp.search( s ) << endl;
+    QString str( args[3]->stringValue() );
 
-  int pos = 0;
-  while ( ( pos = exp.search( s, pos ) ) != -1 )
+    //values retrieved - replace!
+    int pos = 0;
+    while ( ( pos = exp.search( s, pos ) ) != -1 )
+    {
+      int i = exp.matchedLength();
+      s = s.replace( pos, i, str );
+      pos += str.length();
+    }
+  }
+  else
   {
-    int i = exp.matchedLength();
-    s = s.replace( pos, i, str );
-    pos += str.length();
+    if (args.count() > 5)
+      return false;
+    if ( (args.count() >= 4) && ( !KSUtil::checkType( context, args[3], \
KSValue::StringType, true ) )) +      return false;
+    if ( (args.count() == 5) && (!KSUtil::checkType( context, args[4], \
KSValue::IntType, true ) )) +      return false;
+
+    QString defText( (args.count() >= 4) ? args[3]->stringValue() : QString::null );
+
+    int bkref = (args.count() == 5) ? args[4]->intValue() : 0;
+    if (bkref < 0)
+      return false;  //strange back-reference
+  
+    //values retrieved - search!
+    int pos = exp.search( s );
+    if (pos == -1)
+      s = defText;
+    else
+      s = exp.cap (bkref);
   }
 
   context.setValue( new KSValue( s ) );
Index: extensions/text.xml
===================================================================
RCS file: /home/kde/koffice/kspread/extensions/text.xml,v
retrieving revision 1.28
diff -u -3 -p -r1.28 text.xml
--- extensions/text.xml	10 Feb 2004 09:47:07 -0000	1.28
+++ extensions/text.xml	14 Aug 2004 18:25:49 -0000
@@ -566,8 +566,42 @@
         <Syntax>VALUE(text)</Syntax>
         <Example>VALUE("14.03") returns 14.03</Example>
       </Help> 
-    <Function>
+    </Function>
 
+    <Function>
+      <Name>REGEXP</Name>
+      <Type>String</Type>
+      <Parameter>
+        <Comment>Searched text</Comment>
+        <Type range="false">String</Type>
+      </Parameter>
+      <Parameter>
+        <Comment>Regular expression</Comment>
+        <Type range="false">String</Type>
+      </Parameter>
+      <Parameter>
+        <Comment>Replace (default: no)</Comment>
+        <Type range="false">Bool</Type>
+      </Parameter>
+      <Parameter>
+        <Comment>Default value (if finding) / Replacement text (if \
replacing)</Comment> +        <Type range="false">String</Type>
+      </Parameter>
+      <Parameter>
+        <Comment>Back-reference (optional; search ONLY, not used when \
replacing)</Comment> +        <Type range="false">Number</Type>
+      </Parameter>
+      <Help>  
+        <Text>If "replace" is false or not given, returns a part of the string that \
matches a regular expression. If the string doesn't match the given regular \
expression, value specified as default is returned.</Text> +        <Text> If \
"replace" is true, then all matches of a regular expression will be replaced by the \
replacement text.</text> +        <Text>When "replace is false and a back-reference \
is provided, then the value of that back-reference is returned.</Text> +        \
<Text>Also, when "replace" is false and no default value is given, an empty string is \
assumed. If no back-reference is given, 0 is assumed (so that entire matching part is \
returned).</Text> +        <Syntax>REGEXP(text, regexp, replace, default/replacement \
[, backref])</Syntax> +        <Example>REGEXP("Number is   15.";"[0-9]+") = "15" \
</Example> +        <Example>REGEXP("15, 20, 26, 41";"([0-9]+), *[0-9]+$";false;"";1) \
= "26" </Example> +        <Example>REGEXP("14 and 15 and 16";"[0-9]+";true;"num") \
returns "num and num and num"</Example> +      </Help> 
+    </Function>
 
   </Group>
 



_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic