Changing CCalcEngine::s_engineStrings to use string_view for keys (#829)
This commit is contained in:
		
				
					committed by
					
						
						Matt Cooley
					
				
			
			
				
	
			
			
			
						parent
						
							25d7a46ac1
						
					
				
				
					commit
					369843dd37
				
			@@ -24,17 +24,16 @@ static constexpr wstring_view DEFAULT_NUMBER_STR = L"0";
 | 
				
			|||||||
// Read strings for keys, errors, trig types, etc.
 | 
					// Read strings for keys, errors, trig types, etc.
 | 
				
			||||||
// These will be copied from the resources to local memory.
 | 
					// These will be copied from the resources to local memory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
unordered_map<wstring, wstring> CCalcEngine::s_engineStrings;
 | 
					unordered_map<wstring_view, wstring> CCalcEngine::s_engineStrings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void CCalcEngine::LoadEngineStrings(CalculationManager::IResourceProvider& resourceProvider)
 | 
					void CCalcEngine::LoadEngineStrings(CalculationManager::IResourceProvider& resourceProvider)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    for (const auto& sid : g_sids)
 | 
					    for (const auto& sid : g_sids)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        auto locKey = wstring{ sid };
 | 
					        auto locString = resourceProvider.GetCEngineString(sid);
 | 
				
			||||||
        auto locString = resourceProvider.GetCEngineString(locKey);
 | 
					 | 
				
			||||||
        if (!locString.empty())
 | 
					        if (!locString.empty())
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            s_engineStrings[locKey] = locString;
 | 
					            s_engineStrings[sid] = locString;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,10 @@
 | 
				
			|||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
 | 
					// Copyright (c) Microsoft Corporation. All rights reserved.
 | 
				
			||||||
// Licensed under the MIT License.
 | 
					// Licensed under the MIT License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <string_view>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace CalculationManager
 | 
					namespace CalculationManager
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    class IResourceProvider
 | 
					    class IResourceProvider
 | 
				
			||||||
@@ -19,6 +21,6 @@ namespace CalculationManager
 | 
				
			|||||||
        // ids "sDecimal", "sThousand" and "sGrouping". See
 | 
					        // ids "sDecimal", "sThousand" and "sGrouping". See
 | 
				
			||||||
        // https://technet.microsoft.com/en-us/library/cc782655(v=ws.10).aspx
 | 
					        // https://technet.microsoft.com/en-us/library/cc782655(v=ws.10).aspx
 | 
				
			||||||
        // for what these values refer to.
 | 
					        // for what these values refer to.
 | 
				
			||||||
        virtual std::wstring GetCEngineString(const std::wstring& id) = 0;
 | 
					        virtual std::wstring GetCEngineString(std::wstring_view id) = 0;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -98,7 +98,7 @@ public:
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        return s_engineStrings[std::to_wstring(ids)];
 | 
					        return s_engineStrings[std::to_wstring(ids)];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    static std::wstring_view GetString(std::wstring ids)
 | 
					    static std::wstring_view GetString(std::wstring_view ids)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return s_engineStrings[ids];
 | 
					        return s_engineStrings[ids];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -161,7 +161,7 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    std::array<CalcEngine::Rational, NUM_WIDTH_LENGTH> m_chopNumbers;      // word size enforcement
 | 
					    std::array<CalcEngine::Rational, NUM_WIDTH_LENGTH> m_chopNumbers;      // word size enforcement
 | 
				
			||||||
    std::array<std::wstring, NUM_WIDTH_LENGTH> m_maxDecimalValueStrings;   // maximum values represented by a given word width based off m_chopNumbers
 | 
					    std::array<std::wstring, NUM_WIDTH_LENGTH> m_maxDecimalValueStrings;   // maximum values represented by a given word width based off m_chopNumbers
 | 
				
			||||||
    static std::unordered_map<std::wstring, std::wstring> s_engineStrings; // the string table shared across all instances
 | 
					    static std::unordered_map<std::wstring_view, std::wstring> s_engineStrings; // the string table shared across all instances
 | 
				
			||||||
    wchar_t m_decimalSeparator;
 | 
					    wchar_t m_decimalSeparator;
 | 
				
			||||||
    wchar_t m_groupSeparator;
 | 
					    wchar_t m_groupSeparator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ namespace CalculatorApp
 | 
				
			|||||||
        m_resLoader = ResourceLoader::GetForViewIndependentUse("CEngineStrings");
 | 
					        m_resLoader = ResourceLoader::GetForViewIndependentUse("CEngineStrings");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wstring EngineResourceProvider::GetCEngineString(const wstring& id)
 | 
					    wstring EngineResourceProvider::GetCEngineString(wstring_view id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        const auto& localizationSettings = LocalizationSettings::GetInstance();
 | 
					        const auto& localizationSettings = LocalizationSettings::GetInstance();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -43,7 +43,7 @@ namespace CalculatorApp
 | 
				
			|||||||
            return numberGroupingString;
 | 
					            return numberGroupingString;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        StringReference idRef(id.c_str());
 | 
					        StringReference idRef(id.data(), id.length());
 | 
				
			||||||
        String ^ str = m_resLoader->GetString(idRef);
 | 
					        String ^ str = m_resLoader->GetString(idRef);
 | 
				
			||||||
        return str->Begin();
 | 
					        return str->Begin();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,7 +11,7 @@ namespace CalculatorApp
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
        EngineResourceProvider();
 | 
					        EngineResourceProvider();
 | 
				
			||||||
        virtual std::wstring GetCEngineString(const std::wstring& id) override;
 | 
					        virtual std::wstring GetCEngineString(std::wstring_view id) override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
        Windows::ApplicationModel::Resources::ResourceLoader ^ m_resLoader;
 | 
					        Windows::ApplicationModel::Resources::ResourceLoader ^ m_resLoader;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user