[rtems-central commit] content: Add to_camel_case()

Sebastian Huber sebh at rtems.org
Thu Aug 20 14:13:13 UTC 2020


Module:    rtems-central
Branch:    master
Commit:    1d3866a30eea88ff99d4aceda53c4871f61c11c0
Changeset: http://git.rtems.org/rtems-central/commit/?id=1d3866a30eea88ff99d4aceda53c4871f61c11c0

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Aug 11 08:48:25 2020 +0200

content: Add to_camel_case()

---

 rtemsspec/content.py       | 11 +++++++++++
 rtemsspec/sphinxcontent.py | 11 ++---------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/rtemsspec/content.py b/rtemsspec/content.py
index 74b2b43..3c28ab6 100644
--- a/rtemsspec/content.py
+++ b/rtemsspec/content.py
@@ -1031,3 +1031,14 @@ def enabled_by_to_exp(enabled_by: Any, mapper: ExpressionMapper) -> str:
     if exp.startswith("("):
         return exp[1:-1]
     return exp
+
+
+_CAMEL_CASE_TO_UPPER = re.compile(r"\s+(.)")
+_CAMEL_CASE_DISCARD = re.compile(r"[^ \t\n\r\f\va-zA-Z0-9]")
+
+
+def to_camel_case(name: str) -> str:
+    """ Returns the name in CamelCase. """
+    return name[0].upper() + _CAMEL_CASE_TO_UPPER.sub(
+        lambda match: match.group(1).upper(),
+        _CAMEL_CASE_DISCARD.sub(" ", name[1:].replace("+", "X")))
diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py
index 85c7858..914e243 100644
--- a/rtemsspec/sphinxcontent.py
+++ b/rtemsspec/sphinxcontent.py
@@ -25,10 +25,9 @@
 # POSSIBILITY OF SUCH DAMAGE.
 
 from contextlib import contextmanager
-import re
 from typing import Any, Iterable, Iterator, List, Optional, Union
 
-from rtemsspec.content import Content, make_lines
+from rtemsspec.content import Content, make_lines, to_camel_case
 from rtemsspec.items import Item, ItemGetValueContext, ItemMapper
 
 GenericContent = Union[str, List[str], "Content"]
@@ -38,12 +37,6 @@ GenericContentIterable = Union[Iterable[str], Iterable[List[str]],
 _HEADER_LEVELS = ["#", "*", "=", "-", "^", "\""]
 
 
-def _to_camel_case(name: str) -> str:
-    return name[0].upper() + re.sub(
-        r"\s+(.)", lambda match: match.group(1).upper(),
-        re.sub(r"[^ \t\n\r\f\va-zA-Z0-9]", " ", name[1:].replace("+", "X")))
-
-
 def get_reference(label: str, name: Optional[str] = None) -> str:
     """ Returns the reference to the specified label. """
     if name:
@@ -53,7 +46,7 @@ def get_reference(label: str, name: Optional[str] = None) -> str:
 
 def get_label(name: str) -> str:
     """ Returns the label for the specified name. """
-    return _to_camel_case(name.strip())
+    return to_camel_case(name.strip())
 
 
 class SphinxContent(Content):



More information about the vc mailing list