Module: ConsistentCompany

Included in:
String
Defined in:
lib/consistent_company.rb,
lib/consistent_company/version.rb,
ext/consistent_company/consistent_company.c

Defined Under Namespace

Modules: Version

Instance Method Summary collapse

Instance Method Details

#company_namerObject



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/consistent_company/consistent_company.c', line 18

static VALUE rb_CompanyNamer(VALUE self)
{
	char * pSelf = RSTRING_PTR(self);
	int selfLen = (int)strlen(pSelf)+2;
	int workLen = (int)selfLen;
	char * s = pSelf;
	if (*pSelf == '\0')
		return self;
			
	// calc size of work strings
	// while processing we turn & = AND, + = PLUS
	// and we add space at front and back
	while ((s = strpbrk(s, "&+")))
	{	
		workLen +=3; // worst case we add 3 chars
		s++;
	}
	workLen += 90;	// add space front and back
	//////////////
	
	// for company only
	int i;
	char ch;
	int asc;
	int numLefts = 0, numRights = 0;
	int left1 = -1, right1 = -1, left2 = -1, right2 = -1;
	char * workString = malloc(workLen);	// 2 extra chars for TransformCompany
	char * returnString = malloc(workLen);
	char * inString;
	strcpy(workString, pSelf);
	inString = workString;
	
	for( i = 0; inString[i]; i++)
		inString[i] = toupper( inString[i] );
	
	inString = trimwhitespace(inString);
	int len = (int)strlen(inString);
	for (i = 0; i < len; i++)
	{
		if (inString[i] == '(')
		{
			numLefts++;
			if (numLefts == 1)
				left1 = i;
			else if (numLefts == 2)
				left2 = i;
		}
		else if (inString[i] == ')')
		{
			numRights++;
			if (numRights == 1)
				right1 = i;
			else if (numRights == 2)
				right2 = i;
		}
	}

	if (numLefts == 0 || inString[0] == '(')
	{ } // Do Nothing
	else if (numLefts == 1)
	{
		if (right1 > left1)
		{
			// ..(xx)..
			inString[left1++] = ' ';
			memmove(&inString[left1], &inString[right1+1], strlen(inString+right1+1)+1);		
		}
		else
			// ..(xx
			inString[left1] = '\0';
	}
	else if (numLefts == 2)
	{
		if ((left1 < right1) && (right1 < left2) && (left2 < right2))
		{
			// ..(xx)..(xx)..
			inString[left1] = ' ';
			memmove(inString + left1 + 1, inString + right1 + 1, left2-right1-1);
			inString[left1+1+left2-right1-1]  = ' ';
			memmove(inString+left1+1+left2-right1, inString + right2+1, strlen(inString+right2+1)+1);
		}
		else if ((left1 < left2) && (left2 < right1) && (right1 < right2))
		{
			// ..(xx(xx)xx)..
			inString[left1] = ' ';
			memmove(inString+left1+1, inString+right2+1, strlen(inString+right2+1)+1);
		}
		else if ((left1 < right1) && (right1 < left2) && (right2 == -1))
		{
			// ..(xx)..(xx
			inString[left1] = ' ';
			memmove(inString+left1+1, inString+right1+1, left2-right1-1);
			inString[left1+1+left2-right1] = '\0';
		}
		else if ((left1 < left2) && (left2 < right1) && (right2 == -1))
		{
			// ..(xx(xx)xx
			inString[left1] = '\0';
		}
		else if ((right1 == -1) && (right2 == -1))
		{
			// ..(xx(xx
			inString[left1] = '\0';
		}
	}
	char singleCharStr[2];
	singleCharStr[1] = '\0';
	returnString[0] = '\0';
	for (i = 0; i < (int)strlen(inString); i++)
	{
		ch = inString[i];
		asc = (int)ch;

		if ((asc >= 65 && asc <= 90) ||
			(asc >= 48 && asc <= 57) ||
			(asc >= 128))  // A-Z, 0-9, and high order chars
		{
			singleCharStr[0] = ch;
			strcat(returnString, singleCharStr);
		}
		else if (asc == 39) // '
		{ }  // Remove it
		else if (asc == 38 && strlen(returnString) > 0) // &
		{
			if (returnString[strlen(returnString)-1] != ' ')
				strcat(returnString, " AND ");
			else
				strcat(returnString, "AND ");
		}
		else if (asc == 43)  // +
		{
			if (strcmp(returnString, "A") == 0 || strcmp(returnString, "A ") == 0)
				strcpy(returnString, "A PLUS ");
			else if (strlen(returnString) > 0)
			{
				if (returnString[strlen(returnString)-1] != ' ')
					strcat(returnString, " AND ");
				else
					strcat(returnString, "AND ");
			}
		}
		else if (strlen(returnString) > 0 &&
			returnString[strlen(returnString)-1] != ' ')
		{
			strcat(returnString, " ");
		}
	}
	
	str_replace(returnString, " AND ", " & ");
	
	returnString = trimwhitespace(returnString);
	returnString = TransformCompany(returnString);
	VALUE return_value = rb_str_new2(trimwhitespace(returnString));
	free(returnString);
	free(workString);
	return return_value;
}