<!-- SPDX-FileCopyrightText: Copyright 2023 Jetperch LLC
SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<a id="h-topic"></a>

# Topic string utility functions

### *group* Topic manipulation

Topic string utility functions. 

### Defines

### JSDRV_TOPIC_INIT

Empty topic structure initializer. 

### Functions

### void jsdrv_topic_clear(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic)

Clear a topic structure instance to reset it to zero length. 

* **Parameters:**
  **topic** – **[inout]** The topic structure, which is modified in place. 

### void jsdrv_topic_truncate(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic, uint8_t length)

Truncate a topic structure to a specified length. 


If you store length before calling [jsdrv_topic_append()](#group__jsdrv__topic_1ga333bbb0247943f9c015a6675cd0c4337), you can use this function to revert the append. 

* **Parameters:**
  * **topic** – **[inout]** The topic structure, which is modified in place. 
  * **length** – The desired length.

### void jsdrv_topic_append(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic, const char \*subtopic)

Append a subtopic to a topic structure. 


This function intelligently adds the ‘/’ separator. If the topic does not already end with ‘/’, it will be inserted first. 

#### SEE ALSO
[jsdrv_topic_remove](#group__jsdrv__topic_1gac2739877bfcc7fcea145a12cb6c1ed27)

* **Parameters:**
  * **topic** – **[inout]** The topic structure, which is modified in place. 
  * **subtopic** – The subtopic string to add. 

### int32_t jsdrv_topic_remove(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic)

Remove a subtopic from the end of a topic structure. 


This function intelligently removes the ‘/’ separator. If the topic already ends with ‘/’, then this it is ignored. This function then removes the “/subtopic”. The resulting topic does NOT end in ‘/’. 

#### SEE ALSO
[jsdrv_topic_append](#group__jsdrv__topic_1ga333bbb0247943f9c015a6675cd0c4337)

* **Parameters:**
  **topic** – **[inout]** The topic structure, which is modified in place. 
* **Returns:**
  The number of characters removed. 

### void jsdrv_topic_set(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic, const char \*str)

Set the topic to the provided value. 


This function will assert if no room remains. 

* **Parameters:**
  * **topic** – **[inout]** The topic structure, which is modified in place. 
  * **str** – The desired topic value.

### void jsdrv_topic_suffix_add(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic, char ch)

Adds a suffix character to the topic. 


This function will assert if no room remains. 

* **Parameters:**
  * **topic** – **[inout]** The topic structure, which is modified in place. 
  * **ch** – The special character to append.

### char jsdrv_topic_suffix_remove(struct [jsdrv_topic_s](#_CPPv413jsdrv_topic_s) \*topic)

Remove the suffix character from a topic. 

* **Parameters:**
  **topic** – **[inout]** The topic structure, which is modified in place. 
* **Returns:**
  ch The suffix character removed or 0 if no character was removed. 

### struct jsdrv_topic_s

 *#include <topic.h>*

The topic structure. 

### Public Members

### char topic[JSDRV_TOPIC_LENGTH_MAX]

The topic string. 

### uint8_t length

The length in bytes ignoring the null terminator.
