1- using System ;
2- using System . Collections . Generic ;
3- using System . Text ;
4-
5- namespace Vulkan . Build . Codegen
6- {
7- public static class CodegenUtil
8- {
9- private static readonly Dictionary < string , int > s_typeSizes = new Dictionary < string , int >
10- {
11- { "byte" , 1 } ,
12- { "uint" , 4 } ,
13- { "ulong" , 4 } ,
14- { "int" , 4 } ,
15- { "float" , 4 } ,
16- } ;
17-
18- private static readonly HashSet < string > s_keywords = new HashSet < string >
19- {
20- "object" ,
21- "event" ,
22- } ;
23-
24- public static int GetTypeSize ( TypeSpec type )
25- {
26- if ( type . PointerIndirection != 0 || ! s_typeSizes . TryGetValue ( type . Name , out int size ) )
27- {
28- throw new InvalidOperationException ( ) ;
29- }
30-
31- return size ;
32- }
33-
34- public static string NormalizeFieldName ( string name )
35- {
36- if ( s_keywords . Contains ( name ) )
37- {
38- return "@" + name ;
39- }
40-
41- return name ;
42- }
43- public static void SpaceSeparatedList < T > ( CsCodeWriter cw , IList < T > list , Action < T > action )
44- {
45- for ( int i = 0 ; i < list . Count ; i ++ )
46- {
47- var item = list [ i ] ;
48- action ( item ) ;
49- if ( i != list . Count - 1 )
50- {
51- cw . WriteLine ( ) ;
52- }
53- }
54- }
55-
56- private static readonly char [ ] s_underscoreSeparator = { '_' } ;
57-
58- private static readonly HashSet < string > s_preserveCaps = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
59- {
60- "khr" ,
61- "khx" ,
62- "ext" ,
63- "nv" ,
64- "nvx" ,
65- "amd" ,
66- } ;
67-
68-
69- public static string GetPrettyName ( string value , string prefix , HashSet < string > ignoredParts = null )
70- {
71- if ( value . IndexOf ( prefix ) != 0 )
72- {
73- return value ;
74- }
75-
76- string [ ] parts = value . Substring ( prefix . Length , value . Length - prefix . Length )
77- . Split ( s_underscoreSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
78- StringBuilder sb = new StringBuilder ( ) ;
79- foreach ( string part in parts )
80- {
81- if ( ignoredParts != null && ignoredParts . Contains ( part ) )
82- {
83- continue ;
84- }
85-
86- if ( s_preserveCaps . Contains ( part ) )
87- {
88- sb . Append ( part ) ;
89- }
90- else
91- {
92- sb . Append ( char . ToUpper ( part [ 0 ] ) ) ;
93- for ( int i = 1 ; i < part . Length ; i ++ )
94- {
95- sb . Append ( char . ToLower ( part [ i ] ) ) ;
96- }
97- }
98- }
99-
100- string prettyName = sb . ToString ( ) ;
101- return ( char . IsNumber ( prettyName [ 0 ] ) ) ? "_" + prettyName : prettyName ;
102- }
103- }
104- }
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+
5+ namespace Vulkan . Build . Codegen
6+ {
7+ public static class CodegenUtil
8+ {
9+ private static readonly Dictionary < string , int > s_typeSizes = new Dictionary < string , int >
10+ {
11+ { "byte" , 1 } ,
12+ { "uint" , 4 } ,
13+ { "ulong" , 4 } ,
14+ { "int" , 4 } ,
15+ { "float" , 4 } ,
16+ } ;
17+
18+ private static readonly HashSet < string > s_keywords = new HashSet < string >
19+ {
20+ "object" ,
21+ "event" ,
22+ "params"
23+ } ;
24+
25+ public static int GetTypeSize ( TypeSpec type )
26+ {
27+ if ( type . PointerIndirection != 0 || ! s_typeSizes . TryGetValue ( type . Name , out int size ) )
28+ {
29+ throw new InvalidOperationException ( ) ;
30+ }
31+
32+ return size ;
33+ }
34+
35+ public static string NormalizeFieldName ( string name )
36+ {
37+ if ( s_keywords . Contains ( name ) )
38+ {
39+ return "@" + name ;
40+ }
41+
42+ return name ;
43+ }
44+ public static void SpaceSeparatedList < T > ( CsCodeWriter cw , IList < T > list , Action < T > action )
45+ {
46+ for ( int i = 0 ; i < list . Count ; i ++ )
47+ {
48+ var item = list [ i ] ;
49+ action ( item ) ;
50+ if ( i != list . Count - 1 )
51+ {
52+ cw . WriteLine ( ) ;
53+ }
54+ }
55+ }
56+
57+ private static readonly char [ ] s_underscoreSeparator = { '_' } ;
58+
59+ private static readonly HashSet < string > s_preserveCaps = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
60+ {
61+ "khr" ,
62+ "khx" ,
63+ "ext" ,
64+ "nv" ,
65+ "nvx" ,
66+ "amd" ,
67+ } ;
68+
69+
70+ public static string GetPrettyName ( string value , string prefix , HashSet < string > ignoredParts = null )
71+ {
72+ if ( value . IndexOf ( prefix ) != 0 )
73+ {
74+ return value ;
75+ }
76+
77+ string [ ] parts = value . Substring ( prefix . Length , value . Length - prefix . Length )
78+ . Split ( s_underscoreSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
79+ StringBuilder sb = new StringBuilder ( ) ;
80+ foreach ( string part in parts )
81+ {
82+ if ( ignoredParts != null && ignoredParts . Contains ( part ) )
83+ {
84+ continue ;
85+ }
86+
87+ if ( s_preserveCaps . Contains ( part ) )
88+ {
89+ sb . Append ( part ) ;
90+ }
91+ else
92+ {
93+ sb . Append ( char . ToUpper ( part [ 0 ] ) ) ;
94+ for ( int i = 1 ; i < part . Length ; i ++ )
95+ {
96+ sb . Append ( char . ToLower ( part [ i ] ) ) ;
97+ }
98+ }
99+ }
100+
101+ string prettyName = sb . ToString ( ) ;
102+ return ( char . IsNumber ( prettyName [ 0 ] ) ) ? "_" + prettyName : prettyName ;
103+ }
104+ }
105+ }
0 commit comments