Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2022 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 : #pragma once
18 :
19 : #include <lib/dnssd/minimal_mdns/core/QName.h>
20 : #include <lib/support/StringBuilder.h>
21 :
22 : #include <cstring>
23 :
24 : namespace mdns {
25 : namespace Minimal {
26 :
27 : // Allows for a FullQName to be represented as a user-readable logging string
28 : class QNameString
29 : {
30 : public:
31 : QNameString(const mdns::Minimal::FullQName & name);
32 :
33 : QNameString(mdns::Minimal::SerializedQNameIterator name);
34 :
35 326 : inline const char * c_str() const { return mBuffer.c_str(); }
36 :
37 : inline bool Fit() const { return mBuffer.Fit(); }
38 :
39 : template <size_t N>
40 8 : bool EndsWith(const char (&aSuffix)[N]) const
41 : {
42 8 : return EndsWith(&aSuffix[0], N - 1);
43 : }
44 :
45 : private:
46 8 : bool EndsWith(const char * aSuffix, size_t aLength) const
47 : {
48 8 : const char * buffer = mBuffer.c_str();
49 8 : size_t bufferLength = strlen(buffer);
50 8 : if (bufferLength < aLength)
51 : {
52 0 : return false;
53 : }
54 8 : return memcmp(buffer + bufferLength - aLength, aSuffix, aLength) == 0;
55 : }
56 :
57 : static constexpr size_t kMaxQNameLength = 128;
58 : chip::StringBuilder<kMaxQNameLength> mBuffer;
59 : };
60 :
61 : } // namespace Minimal
62 : } // namespace mdns
|