File: chartools.cpp

package info (click to toggle)
kdevelop 4%3A4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,360 kB
  • ctags: 11,484
  • sloc: cpp: 105,371; python: 522; ansic: 488; lex: 422; sh: 139; ruby: 120; makefile: 51; xml: 42; php: 12
file content (127 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
  Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "chartools.h"
#include <QString>
#include <QVector>
#include <util/kdevvarlengtharray.h>
#include <language/duchain/indexedstring.h>
#include <kdebug.h>

QByteArray stringFromContents(const PreprocessedContents& contents, int offset, int count) {
  QByteArray ret;
  for(int a = offset; a < (count ? offset+count : contents.size()); ++a) {
    if(isCharacter(contents[a]))
      ret.append(characterFromIndex(contents[a]));
    else
      ret += KDevelop::IndexedString::fromIndex(contents[a]).byteArray();
  }
  return ret;
}

QByteArray stringFromContents(const uint* contents, int count) {
  QByteArray ret;
  for(int a = 0; a < count; ++a) {
    if(isCharacter(contents[a]))
      ret.append(characterFromIndex(contents[a]));
    else
      ret += KDevelop::IndexedString::fromIndex(contents[a]).byteArray();
  }
  return ret;
}

QByteArray stringFromContentsWithGaps(const PreprocessedContents& contents, int offset, int count) {
  QByteArray ret;
  for(int a = offset; a < (count ? offset+count : contents.size()); ++a) {
    if(isCharacter(contents[a]))
      ret.append(characterFromIndex(contents[a]));
    else
      ret += KDevelop::IndexedString::fromIndex(contents[a]).byteArray();
    ret.append(" ");
  }
  return ret;
}

PreprocessedContents convertFromByteArray(const QByteArray& array) {
  PreprocessedContents to;
  to.resize(array.size());
  const char* data = array.constData();
  const char* dataEnd = data + array.size();
  unsigned int* target = to.data();
  
  
  while(data < dataEnd) {
    *target = indexFromCharacter(*data);
    ++data;
    ++target;
  }
  return to;
}

PreprocessedContents tokenizeFromByteArray(const QByteArray& array) {
  PreprocessedContents to;
  ///testing indicates that 9/10 is about the optimal value
  to.reserve(array.size()/10);//assuming that about every 10 chars is a token.
  const char* data = array.constData();
  const char* dataEnd = data + array.size();
  //unsigned int* target = to.data();
  
  KDevVarLengthArray<char, 100> identifier;
  
  KDevelop::IndexedString::RunningHash hash;

  bool tokenizing = false;
  
  while(data < dataEnd) {
    
    if(!tokenizing) {
      if(isLetter(*data) || *data == '_')
        tokenizing = true;
    }
    
    if(tokenizing) {
      if(isValidMacroIdentifierToken(*data)) {
        hash.append(*data);
        identifier.append(*data);
      }else{
        //End of token
        to.append( KDevelop::IndexedString::indexForString(identifier.constData(), identifier.size(), hash.hash) );
        //kDebug() << "word" << "\"" + KDevelop::IndexedString(to.back()).str() + "\"";
        hash.clear();
        identifier.clear();
        tokenizing = false;
      }
    }
    
    if(!tokenizing)
      to.append( indexFromCharacter(*data) );
    ++data;
  }
  
  if(tokenizing)
    to.append( KDevelop::IndexedString::indexForString(identifier.constData(), identifier.size(), hash.hash) );
  
  
/*  kDebug() << QString::fromUtf8(stringFromContents(to));
  kDebug() << QString::fromUtf8(array);
  Q_ASSERT(stringFromContents(to) == array);*/
  to.squeeze(); 
  return to;
}