Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Words.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include <cctype>
7#include <cstdarg>
8#include <cstdlib>
9#include <iomanip>
10#include <sstream>
11
14#include "Stroika/Foundation/Containers/Common.h"
17
18#include "Words.h"
19
20using namespace Stroika::Foundation;
23
24/*
25 ********************************************************************************
26 ************************************ CapitalizeEachWord ************************
27 ********************************************************************************
28 */
30{
32 // take an ENGLISH string (assume English)
33
34 // toupper each lower-case character preceeded by a space
35 bool prevCharSpace = true; // so we upper first char
36 for (Character i : s) {
37 if (prevCharSpace) {
38 i = i.ToUpperCase ();
39 }
40 r += i;
41 prevCharSpace = i.IsWhitespace ();
42 }
43 return r;
44}
45
46/*
47 ********************************************************************************
48 ******************************* CapitalizeEachSentence *************************
49 ********************************************************************************
50 */
52{
53 // WAY too kludgy - but hopefully adequate for primitive message cleanups...
54 // -- LGP 2008-09-20
56 // take an ENGLISH string (assume English)
57
58 // toupper each lower-case character preceeded by a ENDOFSENTECE PUNCT
59 bool nextCharStartsSentence = true; // so we upper first char
60 for (Character i : s) {
61 if (nextCharStartsSentence and i.IsWhitespace ()) {
62 i = i.ToUpperCase ();
63 nextCharStartsSentence = false;
64 }
65 else {
66 nextCharStartsSentence |= (i == '.' or i == '!' or i == '?');
67 }
68 r += i;
69 }
70 return r;
71}
72
73/*
74 ********************************************************************************
75 ******************************** UnCapitalizeFirstWord *************************
76 ********************************************************************************
77 */
79{
80 // WAY too kludgy - but hopefully adequate for primitive message cleanups...
81 // -- LGP 2008-09-20
82 StringBuilder<StringBuilder_Options<char32_t>> r = s; // char32_t for performant indexing
83 if (r.length () > 2) {
84 if (r[0].ToUpperCase () == r[0] and r[1].ToUpperCase () != r[1]) {
85 r.SetAt (r[0].ToLowerCase (), 0);
86 }
87 }
88 return r;
89}
90
91/*
92 ********************************************************************************
93 ********************************** IsAllCaps ***********************************
94 ********************************************************************************
95 */
96bool Linguistics::IsAllCaps (const String& s)
97{
98 return not s.empty () and s == s.ToUpperCase ();
99}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
nonvirtual size_t length() const noexcept
number of characters, not bytes or code-points
nonvirtual void SetAt(Character item, size_t index) noexcept
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual String ToUpperCase() const
Definition String.cpp:1744
String CapitalizeEachWord(const String &s)
Definition Words.cpp:29
String CapitalizeEachSentence(const String &s)
Definition Words.cpp:51
String UnCapitalizeFirstWord(const String &s)
Definition Words.cpp:78