@@ -271,6 +271,79 @@ extension Expression {
271
271
func manhattanDistance( _ vector: [ Double ] ) -> FunctionExpression {
272
272
return FunctionExpression ( " manhattan_distance " , [ self , Helper . sendableToExpr ( vector) ] )
273
273
}
274
+
275
+ /// Creates an expression that replaces the first occurrence of a literal substring within this
276
+ /// string expression with another literal substring.
277
+ /// Assumes `self` evaluates to a string.
278
+ ///
279
+ /// ```swift
280
+ /// // Replace the first "hello" with "hi" in the "message" field
281
+ /// Field("message").replaceFirst("hello", "hi")
282
+ /// ```
283
+ ///
284
+ /// - Parameter find: The literal string substring to search for.
285
+ /// - Parameter replace: The literal string substring to replace the first occurrence with.
286
+ /// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
287
+ func replaceFirst( _ find: String , with replace: String ) -> FunctionExpression {
288
+ return FunctionExpression (
289
+ " replace_first " ,
290
+ [ self , Helper . sendableToExpr ( find) , Helper . sendableToExpr ( replace) ]
291
+ )
292
+ }
293
+
294
+ /// Creates an expression that replaces the first occurrence of a substring (from an expression)
295
+ /// within this string expression with another substring (from an expression).
296
+ /// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
297
+ ///
298
+ /// ```swift
299
+ /// // Replace first occurrence of field "findPattern" with field "replacePattern" in "text"
300
+ /// Field("text").replaceFirst(Field("findPattern"), Field("replacePattern"))
301
+ /// ```
302
+ ///
303
+ /// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
304
+ /// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace the first
305
+ /// occurrence with.
306
+ /// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
307
+ func replaceFirst( _ find: Expression , with replace: Expression ) -> FunctionExpression {
308
+ return FunctionExpression ( " replace_first " , [ self , find, replace] )
309
+ }
310
+
311
+ /// Creates an expression that replaces all occurrences of a literal substring within this string
312
+ /// expression with another literal substring.
313
+ /// Assumes `self` evaluates to a string.
314
+ ///
315
+ /// ```swift
316
+ /// // Replace all occurrences of " " with "_" in "description"
317
+ /// Field("description").stringReplace(" ", "_")
318
+ /// ```
319
+ ///
320
+ /// - Parameter find: The literal string substring to search for.
321
+ /// - Parameter replace: The literal string substring to replace all occurrences with.
322
+ /// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
323
+ func stringReplace( _ find: String , with replace: String ) -> FunctionExpression {
324
+ return FunctionExpression (
325
+ " string_replace " ,
326
+ [ self , Helper . sendableToExpr ( find) , Helper . sendableToExpr ( replace) ]
327
+ )
328
+ }
329
+
330
+ /// Creates an expression that replaces all occurrences of a substring (from an expression) within
331
+ /// this string expression with another substring (from an expression).
332
+ /// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
333
+ ///
334
+ /// ```swift
335
+ /// // Replace all occurrences of field "target" with field "replacement" in "content"
336
+ /// Field("content").stringReplace(Field("target"), Field("replacement"))
337
+ /// ```
338
+ ///
339
+ /// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
340
+ /// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace all
341
+ /// occurrences with.
342
+ /// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
343
+ func stringReplace( _ find: Expression , with replace: Expression ) -> FunctionExpression {
344
+ return FunctionExpression ( " string_replace " , [ self , find, replace] )
345
+ }
346
+
274
347
}
275
348
276
349
public extension Expression {
@@ -594,28 +667,6 @@ public extension Expression {
594
667
return FunctionExpression ( " reverse " , [ self ] )
595
668
}
596
669
597
- func replaceFirst( _ find: String , with replace: String ) -> FunctionExpression {
598
- return FunctionExpression (
599
- " replace_first " ,
600
- [ self , Helper . sendableToExpr ( find) , Helper . sendableToExpr ( replace) ]
601
- )
602
- }
603
-
604
- func replaceFirst( _ find: Expression , with replace: Expression ) -> FunctionExpression {
605
- return FunctionExpression ( " replace_first " , [ self , find, replace] )
606
- }
607
-
608
- func replaceAll( _ find: String , with replace: String ) -> FunctionExpression {
609
- return FunctionExpression (
610
- " replace_all " ,
611
- [ self , Helper . sendableToExpr ( find) , Helper . sendableToExpr ( replace) ]
612
- )
613
- }
614
-
615
- func replaceAll( _ find: Expression , with replace: Expression ) -> FunctionExpression {
616
- return FunctionExpression ( " replace_all " , [ self , find, replace] )
617
- }
618
-
619
670
func byteLength( ) -> FunctionExpression {
620
671
return FunctionExpression ( " byte_length " , [ self ] )
621
672
}
0 commit comments