Skip to content

Commit daf020e

Browse files
committed
Much improved snake case conversion
Fixes #65
1 parent 574a5ea commit daf020e

14 files changed

+825
-781
lines changed

Generator/NameHelpers.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Text;
4+
using System.Text.RegularExpressions;
45

56
namespace Generator
67
{
@@ -21,23 +22,66 @@ public static string FirstToLower(string name)
2122
return name[0].ToString().ToLower() + name.Substring(1);
2223
}
2324

25+
static Regex CaseConversionExceptionsRegex = new Regex(
26+
"(Base64|Float32|Float64|DBm|UInt|SInt|SFloat|Direct3D|VCard|OAuth|CData|ESim|MPeg|WMAudio|URLs|EFSpn|EFOns|EFOpl|EFPnn|ETag|IndexedDB|JavaScript|HResult|UIElement|IMediaSource|IAnimationObject|IBuffer)" +
27+
"([A-Z0-9_]|$)", RegexOptions.Compiled);
28+
2429
public static string CamelToSnakeCase(string name)
2530
{
2631
var newName = new StringBuilder();
2732
bool noUnderscore = true;
2833
bool previousUpper = false;
29-
foreach (var c in name)
34+
bool inNumeric = false;
35+
uint distPrevUpper = uint.MaxValue; // distance to previous uppercase letter
36+
37+
int ci = 0;
38+
39+
Func<int, bool> isLower = (offset) => ci + offset < name.Length && ci + offset >= 0 && char.IsLower(name[ci + offset]);
40+
Func<int, bool> isUpper = (offset) => ci + offset < name.Length && ci + offset >= 0 && char.IsUpper(name[ci + offset]);
41+
Func<int, bool> isDigit = (offset) => ci + offset < name.Length && ci + offset >= 0 && char.IsDigit(name[ci + offset]);
42+
43+
var matches = CaseConversionExceptionsRegex.Matches(name).Cast<Match>().Select(m => m.Captures[0]).ToArray();
44+
Func<bool> withinMatch = () => matches.Any(m => ci > m.Index && ci < (m.Index + m.Length - 1));
45+
46+
for (; ci < name.Length; ci++)
3047
{
31-
if (char.IsUpper(c))
48+
var c = name[ci];
49+
if (isUpper(0))
3250
{
33-
if (!noUnderscore && !previousUpper) newName.Append("_");
51+
distPrevUpper = 0;
52+
if (!noUnderscore && (!previousUpper || isLower(1)) && !withinMatch())
53+
newName.Append("_");
3454
newName.Append(char.ToLowerInvariant(c));
3555
previousUpper = true;
56+
inNumeric = false;
57+
}
58+
else if (isDigit(0) && (isDigit(1) || (isUpper(1) && !isLower(2))))
59+
{
60+
distPrevUpper += 1;
61+
bool seen4LetterException = false;
62+
if (distPrevUpper == 4)
63+
{
64+
var last4 = name.Substring(ci - 4, 4);
65+
seen4LetterException = last4 == "Char" || last4 == "Argb" || last4 == "Wtls" || last4 == "Ieee";
66+
}
67+
bool seen2LetterException = false;
68+
if (ci >= 2)
69+
{
70+
var last2 = name.Substring(ci - 2, 2);
71+
seen2LetterException = last2 == "Is" || last2 == "3D";
72+
}
73+
if (!noUnderscore && !inNumeric && ((distPrevUpper > 3 && !seen4LetterException) || seen2LetterException) && !withinMatch())
74+
newName.Append("_");
75+
newName.Append(c);
76+
previousUpper = true;
77+
inNumeric = true;
3678
}
3779
else
3880
{
81+
distPrevUpper += 1;
3982
newName.Append(c);
4083
previousUpper = false;
84+
inNumeric = inNumeric && (isDigit(0) || isDigit(1)); // stay in numeric mode if there's just one letter in between
4185
}
4286

4387
noUnderscore = false;

src/rt/gen/windows/ai.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl ILearningModelDevice {
194194
let hr = ((*self.lpVtbl).get_AdapterId)(self as *const _ as *mut _, &mut out);
195195
if hr == S_OK { Ok(out) } else { err(hr) }
196196
}}
197-
#[cfg(feature="windows-graphics")] #[inline] pub fn get_direct3_d11_device(&self) -> Result<Option<ComPtr<super::super::graphics::directx::direct3d11::IDirect3DDevice>>> { unsafe {
197+
#[cfg(feature="windows-graphics")] #[inline] pub fn get_direct3d_11_device(&self) -> Result<Option<ComPtr<super::super::graphics::directx::direct3d11::IDirect3DDevice>>> { unsafe {
198198
let mut out = null_mut();
199199
let hr = ((*self.lpVtbl).get_Direct3D11Device)(self as *const _ as *mut _, &mut out);
200200
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
@@ -207,8 +207,8 @@ impl LearningModelDevice {
207207
#[inline] pub fn create(deviceKind: LearningModelDeviceKind) -> Result<ComPtr<LearningModelDevice>> {
208208
<Self as RtActivatable<ILearningModelDeviceFactory>>::get_activation_factory().create(deviceKind)
209209
}
210-
#[cfg(feature="windows-graphics")] #[inline] pub fn create_from_direct3_d11_device(device: &super::super::graphics::directx::direct3d11::IDirect3DDevice) -> Result<Option<ComPtr<LearningModelDevice>>> {
211-
<Self as RtActivatable<ILearningModelDeviceStatics>>::get_activation_factory().create_from_direct3_d11_device(device)
210+
#[cfg(feature="windows-graphics")] #[inline] pub fn create_from_direct3d_11_device(device: &super::super::graphics::directx::direct3d11::IDirect3DDevice) -> Result<Option<ComPtr<LearningModelDevice>>> {
211+
<Self as RtActivatable<ILearningModelDeviceStatics>>::get_activation_factory().create_from_direct3d_11_device(device)
212212
}
213213
}
214214
DEFINE_CLSID!(LearningModelDevice(&[87,105,110,100,111,119,115,46,65,73,46,77,97,99,104,105,110,101,76,101,97,114,110,105,110,103,46,76,101,97,114,110,105,110,103,77,111,100,101,108,68,101,118,105,99,101,0]) [CLSID_LearningModelDevice]);
@@ -231,7 +231,7 @@ RT_INTERFACE!{static interface ILearningModelDeviceStatics(ILearningModelDeviceS
231231
#[cfg(feature="windows-graphics")] fn CreateFromDirect3D11Device(&self, device: *mut super::super::graphics::directx::direct3d11::IDirect3DDevice, out: *mut *mut LearningModelDevice) -> HRESULT
232232
}}
233233
impl ILearningModelDeviceStatics {
234-
#[cfg(feature="windows-graphics")] #[inline] pub fn create_from_direct3_d11_device(&self, device: &super::super::graphics::directx::direct3d11::IDirect3DDevice) -> Result<Option<ComPtr<LearningModelDevice>>> { unsafe {
234+
#[cfg(feature="windows-graphics")] #[inline] pub fn create_from_direct3d_11_device(&self, device: &super::super::graphics::directx::direct3d11::IDirect3DDevice) -> Result<Option<ComPtr<LearningModelDevice>>> { unsafe {
235235
let mut out = null_mut();
236236
let hr = ((*self.lpVtbl).CreateFromDirect3D11Device)(self as *const _ as *mut _, device as *const _ as *mut _, &mut out);
237237
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }

src/rt/gen/windows/applicationmodel.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15248,12 +15248,12 @@ impl IDataPackagePropertySet2 {
1524815248
let hr = ((*self.lpVtbl).put_PackageFamilyName)(self as *const _ as *mut _, value.get());
1524915249
if hr == S_OK { Ok(()) } else { err(hr) }
1525015250
}}
15251-
#[cfg(feature="windows-storage")] #[inline] pub fn get_square30x30_logo(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
15251+
#[cfg(feature="windows-storage")] #[inline] pub fn get_square_30x30_logo(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
1525215252
let mut out = null_mut();
1525315253
let hr = ((*self.lpVtbl).get_Square30x30Logo)(self as *const _ as *mut _, &mut out);
1525415254
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
1525515255
}}
15256-
#[cfg(feature="windows-storage")] #[inline] pub fn set_square30x30_logo(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
15256+
#[cfg(feature="windows-storage")] #[inline] pub fn set_square_30x30_logo(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
1525715257
let hr = ((*self.lpVtbl).put_Square30x30Logo)(self as *const _ as *mut _, value as *const _ as *mut _);
1525815258
if hr == S_OK { Ok(()) } else { err(hr) }
1525915259
}}
@@ -15367,7 +15367,7 @@ impl IDataPackagePropertySetView2 {
1536715367
let hr = ((*self.lpVtbl).get_ContentSourceApplicationLink)(self as *const _ as *mut _, &mut out);
1536815368
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
1536915369
}}
15370-
#[cfg(feature="windows-storage")] #[inline] pub fn get_square30x30_logo(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
15370+
#[cfg(feature="windows-storage")] #[inline] pub fn get_square_30x30_logo(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
1537115371
let mut out = null_mut();
1537215372
let hr = ((*self.lpVtbl).get_Square30x30Logo)(self as *const _ as *mut _, &mut out);
1537315373
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
@@ -15698,8 +15698,8 @@ impl DataTransferManager {
1569815698
#[inline] pub fn is_supported() -> Result<bool> {
1569915699
<Self as RtActivatable<IDataTransferManagerStatics2>>::get_activation_factory().is_supported()
1570015700
}
15701-
#[inline] pub fn show_share_uiwith_options(options: &ShareUIOptions) -> Result<()> {
15702-
<Self as RtActivatable<IDataTransferManagerStatics3>>::get_activation_factory().show_share_uiwith_options(options)
15701+
#[inline] pub fn show_share_ui_with_options(options: &ShareUIOptions) -> Result<()> {
15702+
<Self as RtActivatable<IDataTransferManagerStatics3>>::get_activation_factory().show_share_ui_with_options(options)
1570315703
}
1570415704
}
1570515705
DEFINE_CLSID!(DataTransferManager(&[87,105,110,100,111,119,115,46,65,112,112,108,105,99,97,116,105,111,110,77,111,100,101,108,46,68,97,116,97,84,114,97,110,115,102,101,114,46,68,97,116,97,84,114,97,110,115,102,101,114,77,97,110,97,103,101,114,0]) [CLSID_DataTransferManager]);
@@ -15751,7 +15751,7 @@ RT_INTERFACE!{static interface IDataTransferManagerStatics3(IDataTransferManager
1575115751
fn ShowShareUIWithOptions(&self, options: *mut ShareUIOptions) -> HRESULT
1575215752
}}
1575315753
impl IDataTransferManagerStatics3 {
15754-
#[inline] pub fn show_share_uiwith_options(&self, options: &ShareUIOptions) -> Result<()> { unsafe {
15754+
#[inline] pub fn show_share_ui_with_options(&self, options: &ShareUIOptions) -> Result<()> { unsafe {
1575515755
let hr = ((*self.lpVtbl).ShowShareUIWithOptions)(self as *const _ as *mut _, options as *const _ as *mut _);
1575615756
if hr == S_OK { Ok(()) } else { err(hr) }
1575715757
}}
@@ -16248,20 +16248,20 @@ impl ICoreDragOperation {
1624816248
let hr = ((*self.lpVtbl).SetPointerId)(self as *const _ as *mut _, pointerId);
1624916249
if hr == S_OK { Ok(()) } else { err(hr) }
1625016250
}}
16251-
#[cfg(feature="windows-graphics")] #[inline] pub fn set_drag_uicontent_from_software_bitmap(&self, softwareBitmap: &::rt::gen::windows::graphics::imaging::SoftwareBitmap) -> Result<()> { unsafe {
16251+
#[cfg(feature="windows-graphics")] #[inline] pub fn set_drag_ui_content_from_software_bitmap(&self, softwareBitmap: &::rt::gen::windows::graphics::imaging::SoftwareBitmap) -> Result<()> { unsafe {
1625216252
let hr = ((*self.lpVtbl).SetDragUIContentFromSoftwareBitmap)(self as *const _ as *mut _, softwareBitmap as *const _ as *mut _);
1625316253
if hr == S_OK { Ok(()) } else { err(hr) }
1625416254
}}
16255-
#[cfg(feature="windows-graphics")] #[inline] pub fn set_drag_uicontent_from_software_bitmap_with_anchor_point(&self, softwareBitmap: &::rt::gen::windows::graphics::imaging::SoftwareBitmap, anchorPoint: foundation::Point) -> Result<()> { unsafe {
16255+
#[cfg(feature="windows-graphics")] #[inline] pub fn set_drag_ui_content_from_software_bitmap_with_anchor_point(&self, softwareBitmap: &::rt::gen::windows::graphics::imaging::SoftwareBitmap, anchorPoint: foundation::Point) -> Result<()> { unsafe {
1625616256
let hr = ((*self.lpVtbl).SetDragUIContentFromSoftwareBitmapWithAnchorPoint)(self as *const _ as *mut _, softwareBitmap as *const _ as *mut _, anchorPoint);
1625716257
if hr == S_OK { Ok(()) } else { err(hr) }
1625816258
}}
16259-
#[inline] pub fn get_drag_uicontent_mode(&self) -> Result<CoreDragUIContentMode> { unsafe {
16259+
#[inline] pub fn get_drag_ui_content_mode(&self) -> Result<CoreDragUIContentMode> { unsafe {
1626016260
let mut out = zeroed();
1626116261
let hr = ((*self.lpVtbl).get_DragUIContentMode)(self as *const _ as *mut _, &mut out);
1626216262
if hr == S_OK { Ok(out) } else { err(hr) }
1626316263
}}
16264-
#[inline] pub fn set_drag_uicontent_mode(&self, value: CoreDragUIContentMode) -> Result<()> { unsafe {
16264+
#[inline] pub fn set_drag_ui_content_mode(&self, value: CoreDragUIContentMode) -> Result<()> { unsafe {
1626516265
let hr = ((*self.lpVtbl).put_DragUIContentMode)(self as *const _ as *mut _, value);
1626616266
if hr == S_OK { Ok(()) } else { err(hr) }
1626716267
}}
@@ -29197,30 +29197,30 @@ impl IWalletItem {
2919729197
let hr = ((*self.lpVtbl).put_ExpirationDate)(self as *const _ as *mut _, value as *const _ as *mut _);
2919829198
if hr == S_OK { Ok(()) } else { err(hr) }
2919929199
}}
29200-
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo159x159(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
29200+
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo_159x159(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
2920129201
let mut out = null_mut();
2920229202
let hr = ((*self.lpVtbl).get_Logo159x159)(self as *const _ as *mut _, &mut out);
2920329203
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
2920429204
}}
29205-
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo159x159(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
29205+
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo_159x159(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
2920629206
let hr = ((*self.lpVtbl).put_Logo159x159)(self as *const _ as *mut _, value as *const _ as *mut _);
2920729207
if hr == S_OK { Ok(()) } else { err(hr) }
2920829208
}}
29209-
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo336x336(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
29209+
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo_336x336(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
2921029210
let mut out = null_mut();
2921129211
let hr = ((*self.lpVtbl).get_Logo336x336)(self as *const _ as *mut _, &mut out);
2921229212
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
2921329213
}}
29214-
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo336x336(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
29214+
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo_336x336(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
2921529215
let hr = ((*self.lpVtbl).put_Logo336x336)(self as *const _ as *mut _, value as *const _ as *mut _);
2921629216
if hr == S_OK { Ok(()) } else { err(hr) }
2921729217
}}
29218-
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo99x99(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
29218+
#[cfg(feature="windows-storage")] #[inline] pub fn get_logo_99x99(&self) -> Result<Option<ComPtr<super::super::storage::streams::IRandomAccessStreamReference>>> { unsafe {
2921929219
let mut out = null_mut();
2922029220
let hr = ((*self.lpVtbl).get_Logo99x99)(self as *const _ as *mut _, &mut out);
2922129221
if hr == S_OK { Ok(ComPtr::wrap_optional(out)) } else { err(hr) }
2922229222
}}
29223-
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo99x99(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
29223+
#[cfg(feature="windows-storage")] #[inline] pub fn set_logo_99x99(&self, value: &super::super::storage::streams::IRandomAccessStreamReference) -> Result<()> { unsafe {
2922429224
let hr = ((*self.lpVtbl).put_Logo99x99)(self as *const _ as *mut _, value as *const _ as *mut _);
2922529225
if hr == S_OK { Ok(()) } else { err(hr) }
2922629226
}}

0 commit comments

Comments
 (0)