xref: /haiku/headers/os/support/StringList.h (revision 1fe24d0cd0b547a771c00f6fca8f50ba6ca2fb2c)
1 /*
2  * Copyright 2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SUPPORT_BSTRING_LIST_H_
6 #define _SUPPORT_BSTRING_LIST_H_
7 
8 
9 #include <BeBuild.h>
10 #include <List.h>
11 #include <String.h>
12 
13 
14 class BStringList {
15 public:
16 								BStringList(int32 count = 20);
17 								BStringList(const BStringList& other);
18 	virtual						~BStringList();
19 
20 	// Adding and removing items.
21 			bool				Add(const BString& string, int32 index);
22 			bool				Add(const BString& string);
23 			bool				Add(const BStringList& list, int32 index);
24 			bool				Add(const BStringList& list);
25 
26 			bool				Remove(const BString& string,
27 									bool ignoreCase = false);
28 			BString				Remove(int32 index);
29 			bool				Remove(int32 index, int32 count);
30 			bool				Replace(int32 index, const BString& string);
31 
32 			void				MakeEmpty();
33 
34 	// Reorder items
35 			void				Sort(bool ignoreCase = false);
36 									// TODO: Sort() with custom sort function.
37 			bool				Swap(int32 indexA, int32 indexB);
38 			bool				Move(int32 fromIndex, int32 toIndex);
39 
40 	// Retrieve items
41 			BString				StringAt(int32 index) const;
42 			BString				First() const;
43 			BString				Last() const;
44 
45 	// Query
46 			bool				HasString(const BString& string,
47 									bool ignoreCase = false) const;
48 			int32				IndexOf(const BString& string,
49 									bool ignoreCase = false) const;
50 			int32				CountStrings() const;
51 			bool				IsEmpty() const;
52 
53 	// Iteration
54 			void				DoForEach(bool (*func)(const BString& string));
55 			void				DoForEach(bool (*func)(const BString& string,
56 									void* arg2), void* arg2);
57 
58 			BStringList&		operator=(const BStringList& other);
59 			bool				operator==(const BStringList& other) const;
60 			bool				operator!=(const BStringList& other) const;
61 
62 private:
63 			void				_IncrementRefCounts() const;
64 			void				_DecrementRefCounts() const;
65 
66 private:
67 			BList				fStrings;
68 };
69 
70 
71 inline bool
72 BStringList::HasString(const BString& string, bool ignoreCase) const
73 {
74 	return IndexOf(string, ignoreCase) >= 0;
75 }
76 
77 
78 inline bool
79 BStringList::operator!=(const BStringList& other) const
80 {
81 	return !(*this == other);
82 }
83 
84 
85 #endif	// _SUPPORT_BSTRING_LIST_H_
86