Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
macaon2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexis Nasr
macaon2
Commits
c63e1d70
Commit
c63e1d70
authored
8 years ago
by
Alexis Nasr
Browse files
Options
Downloads
Patches
Plain Diff
added char16 in maca_common to process utf8 strings
parent
6b4af9bf
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
maca_common/src/char16.c
+94
-0
94 additions, 0 deletions
maca_common/src/char16.c
with
94 additions
and
0 deletions
maca_common/src/char16.c
0 → 100644
+
94
−
0
View file @
c63e1d70
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
typedef
short
char16
;
#define char_bit1(c) ((c) & 1)
#define char_bit2(c) (((c) & 2) >> 1)
#define char_bit3(c) (((c) & 4) >> 2)
#define char_bit4(c) (((c) & 8) >> 3)
#define char_bit5(c) (((c) & 16) >> 4)
#define char_bit6(c) (((c) & 32) >> 5)
#define char_bit7(c) (((c) & 64) >> 6)
#define char_bit8(c) (((c) & 128) >> 7)
#define length(c) ((!char_bit8((c)) || (char_bit8(c) && !char_bit7(c)))? 1 : 2)
/*
int length(char c)
{
if(!char_bit8(c)) return 1;
if(char_bit8(c) && !char_bit7(c)) return 1;
if(char_bit7(c)) return 2;
if(char_bit6(c)) return 3;
if(char_bit5(4)) return 4;
}
*/
int
utf8_strlen
(
char
*
utf8_string
)
{
int
l
=
0
;
while
(
*
utf8_string
){
l
+=
(
length
(
*
utf8_string
)
==
1
)
?
1
:
0
;
utf8_string
++
;
}
return
l
;
}
char
*
char16toutf8
(
char16
*
char16_string
)
{
}
int
char16_strlen
(
char16
*
string
)
{
int
i
=
0
;
while
(
string
[
i
])
i
++
;
return
i
;
}
char16
*
utf8tochar16
(
char
*
utf8_string
)
{
char16
c16
;
int
i
,
j
;
int
utf8_length
=
strlen
(
utf8_string
);
int
char16_length
=
0
;
char16
*
char16_string
;
for
(
i
=
0
;
i
<
utf8_length
;
i
++
)
char16_length
+=
length
(
utf8_string
[
i
]);
char16_string
=
malloc
((
char16_length
+
1
)
*
sizeof
(
char
));
for
(
i
=
0
,
j
=
0
;
i
<
utf8_length
;
i
++
,
j
++
){
if
(
length
(
utf8_string
[
i
])
==
1
){
char16_string
[
j
]
=
(
char16
)
utf8_string
[
i
];
}
if
(
length
(
utf8_string
[
i
])
==
2
){
char16_string
[
j
]
=
utf8_string
[
i
];
char16_string
[
j
]
<<
8
;
char16_string
[
j
]
+=
utf8_string
[
++
i
];
}
}
char16_string
[
j
]
=
0
;
return
char16_string
;
}
int
main
(
void
)
{
int
i
;
char
string
[
200
];
char16
*
char16_string
;
strcpy
(
string
,
"élémentaire"
);
/* strcpy(string, "konjunktúra-időszaknál"); */
printf
(
"string = %s
\n
"
,
string
);
printf
(
"length = %d
\n
"
,
(
int
)
strlen
(
string
));
printf
(
"utf8 length = %d
\n
"
,
(
int
)
utf8_strlen
(
string
));
for
(
i
=
0
;
i
<
strlen
(
string
);
i
++
){
printf
(
"%d
\t
%c
\t
%d
\t
%d
\t
%d
\t
%d
\t
%d
\t
%d
\t
%d
\t
%d
\t
%d
\t
l=%d
\n
"
,
i
,
string
[
i
],
(
int
)
string
[
i
],
char_bit1
(
string
[
i
]),
char_bit2
(
string
[
i
]),
char_bit3
(
string
[
i
]),
char_bit4
(
string
[
i
]),
char_bit5
(
string
[
i
]),
char_bit6
(
string
[
i
]),
char_bit7
(
string
[
i
]),
char_bit8
(
string
[
i
]),
length
(
string
[
i
]));
}
char16_string
=
utf8tochar16
(
string
);
printf
(
"char16_strlen = %d
\n
"
,
char16_strlen
(
char16_string
));
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment