Skip to content

Commit 555003a

Browse files
author
aafent
committed
New function singlewhite() to Text Replacer Library
1 parent 37cda18 commit 555003a

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

FAST.FBasic.InteractiveConsole/Tests/textReplacer.bas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ print words(outtext,1)+" Words in total"
3434
print words(outtext,2)+" Words > than 2 characters in length"
3535
print words(outtext,4)+" Words > than 4 characters in length"
3636

37+
print len(outtext)+" Characters in length ";
38+
print len(singlewhite(outtext))+" characters signle white spaces."
39+
40+
3741
halt
3842

3943
rem

FAST.FBasicInterpreter/Libraries/FBasicTextReplacer.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
/// pcase(string) :: Converts the string to Proper Case (first letter upper rest lower)
1616
/// words(s, 1) :: counts the number of words in a string s that are at least 1 character long. if the input string is empty returns 0, if the minWordSize is less than 1 it is set to 1. The word delimiters are space, tab, newline and carriage-return. Consequent delimiters are treated as one.
1717
/// phtoname(s) :: from a placeholder keeps only the variable name (simple or squear bracket)
18-
18+
/// singlewhite(s) :: return the input string without duplicate white spaces (newlines etc).
19+
///
1920

2021
///
2122
/// Placeholders are defined as {name}, where name is the name of a variable.
@@ -36,6 +37,12 @@
3637
/// </summary>
3738
public class FBasicTextReplacer : IFBasicLibrary
3839
{
40+
// We can pre-compile the Regex for better performance if this method
41+
// is called very frequently. For .NET 7+, we can use [GeneratedRegex].
42+
// For a simple static method, Regex.Replace is clear and efficient.
43+
private static readonly Regex _whitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
44+
45+
3946
public void InstallAll(IInterpreter interpreter)
4047
{
4148
interpreter.AddStatement("PHREPLACE", placeHolderReplace);
@@ -47,6 +54,7 @@ public void InstallAll(IInterpreter interpreter)
4754
interpreter.AddFunction("pcase", PCase); // Proper Case
4855
interpreter.AddFunction("words", Words); // words count
4956
interpreter.AddFunction("phtoname",ToName);
57+
interpreter.AddFunction("singlewhite",SignleWhite);
5058
}
5159

5260
#region (+) FBASIC Statments
@@ -175,7 +183,6 @@ private static void WordFrequency(IInterpreter interpreter)
175183

176184
#endregion (+) FBASIC Statements
177185

178-
179186
#region (+) Supporting methods
180187

181188
private static void placeHolderFilter(IInterpreter interpreter, bool dottedOnly)
@@ -378,6 +385,28 @@ private static Value Words(IInterpreter interpreter, List<Value> args)
378385
return new Value(CountLongWords(str, size));
379386
}
380387

388+
389+
/// <summary>
390+
/// FBASIC Function Words()
391+
/// </summary>
392+
/// <param name="interpreter"></param>
393+
/// <param name="args"></param>
394+
/// <returns></returns>
395+
private static Value SignleWhite(IInterpreter interpreter, List<Value> args)
396+
{
397+
// Syntax: singlewhite(s) : remove all duplicate characters are white spaces (such as space,tab, newline etc).
398+
// Used to shorten a string when the lines and spaces are not necessary (for example prompts to AI).
399+
//
400+
string syntax = "singlewhite(string)";
401+
if (args.Count != 1)
402+
return interpreter.Error("SINGLEWHITE", Errors.E125_WrongNumberOfArguments(1, syntax)).value;
403+
404+
string str = args[0].Convert(FAST.FBasicInterpreter.ValueType.String).String;
405+
406+
return new Value(RemoveDuplicateWhitespace(str));
407+
}
408+
409+
381410
private static Value ToName(IInterpreter interpreter, List<Value> args)
382411
{
383412
string syntax = "phtoname(string)";
@@ -398,6 +427,8 @@ private static Value ToName(IInterpreter interpreter, List<Value> args)
398427

399428
#endregion (+) FBASIC Functions
400429

430+
431+
401432
#region (+) Public static methods
402433

403434

@@ -477,7 +508,50 @@ public static Dictionary<string, int> CalculateWordFrequency(string input, int m
477508
}
478509

479510

511+
/// <summary>
512+
/// Takes an input string and replaces any sequence of one or more
513+
/// whitespace characters (space, tab, carriage return, newline)
514+
/// with a single space character.
515+
/// </summary>
516+
/// <param name="input">The string to process.</param>
517+
/// <returns>A copy of the string with consolidated whitespace.</returns>
518+
public static string RemoveDuplicateWhitespace(string input)
519+
{
520+
/* Note: In .NET's RegEx engine, \s is a special shorthand that matches any whitespace character.
521+
This explicitly includes:
522+
Space ( )
523+
Tab (\t)
524+
New-line (\n)
525+
Carriage return (\r)
526+
Form feed (\f)
527+
Vertical tab (\v)
528+
*/
529+
530+
// Handle null or empty strings gracefully
531+
if (string.IsNullOrEmpty(input)) return input;
532+
533+
// The \s character class in RegEx matches any whitespace character,
534+
// including space, \t (tab), \r (carriage return), and \n (newline).
535+
// The + quantifier means "one or more" of the preceding element.
536+
// So, \s+ finds all sequences of one or more whitespace chars.
537+
// We replace each found sequence with a single space " ".
538+
539+
// Using the static Regex.Replace method:
540+
return Regex.Replace(input, @"\s+", " ");
541+
542+
// Alternatively, using the pre-compiled instance:
543+
// return _whitespaceRegex.Replace(input, " ");
544+
}
545+
546+
480547
#endregion (+) Public static methods
481548

482549

483550
}
551+
552+
553+
554+
555+
556+
557+

FAST.FBasicInterpreter/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ For **Business and Technical** documentation read the [Wiki page](https://github
99

1010
| When | Description |
1111
|------------|--------------------------------------------------------|
12+
| 2025-11-03 | New function singlewhite() to Text Replacer Library |
1213
| 2025-11-01 | New Packages versions (Interpreter,Toolkit) |
1314
| 2025-11-01 | New library for basic Json operations. |
1415
| 2025-10-29 | Refactor of the RequestForObject handler |

0 commit comments

Comments
 (0)