Merged with trunk R1283.
[ardour.git] / libs / pbd / whitespace.cc
1 #include <pbd/whitespace.h>
2
3 using namespace std;
4
5 namespace PBD {
6         
7 void
8 strip_whitespace_edges (string& str)
9 {   
10     string::size_type i; 
11     string::size_type len;    
12     string::size_type s;
13                                     
14     len = str.length();
15
16     if (len == 1) {
17             return;
18     }
19
20     /* strip front */
21                                         
22     for (i = 0; i < len; ++i) {
23         if (isgraph (str[i])) {
24             break;
25         }
26     }
27
28     if (i == len) {
29             /* it's all whitespace, not much we can do */
30                 str = "";
31             return;
32     }
33
34     /* strip back */
35     
36     if (len > 1) {
37     
38             s = i;
39             i = len - 1;
40
41             if (s == i) {
42                     return;
43             }
44             
45             do {
46                     if (isgraph (str[i]) || i == 0) {
47                             break;
48                     }
49
50                     --i;
51
52             } while (true); 
53             
54             str = str.substr (s, (i - s) + 1);
55
56     } else {
57             str = str.substr (s);
58     }
59 }
60
61 } // namespace PBD