StaticString.pm 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2013 Google, Inc. All Rights Reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  13. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  16. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  20. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. package StaticString;
  24. use strict;
  25. use Hasher;
  26. sub GenerateStrings($)
  27. {
  28. my $stringsRef = shift;
  29. my %strings = %$stringsRef;
  30. my @result = ();
  31. while ( my ($name, $value) = each %strings ) {
  32. push(@result, "static const LChar ${name}String8[] = \"${value}\";\n");
  33. }
  34. push(@result, "\n");
  35. while ( my ($name, $value) = each %strings ) {
  36. my $length = length($value);
  37. my $hash = Hasher::GenerateHashValue($value);
  38. push(@result, <<END);
  39. static StringImpl::StaticASCIILiteral ${name}Data = {
  40. StringImpl::StaticASCIILiteral::s_initialRefCount,
  41. $length,
  42. ${name}String8,
  43. 0,
  44. StringImpl::StaticASCIILiteral::s_initialFlags | (${hash} << StringImpl::StaticASCIILiteral::s_hashShift)
  45. };
  46. END
  47. }
  48. push(@result, "\n");
  49. while ( my ($name, $value) = each %strings ) {
  50. push(@result, "static StringImpl* ${name}Impl = reinterpret_cast<StringImpl*>(&${name}Data);\n");
  51. }
  52. push(@result, "\n");
  53. return join "", @result;
  54. }
  55. sub GenerateStringAsserts($)
  56. {
  57. my $stringsRef = shift;
  58. my %strings = %$stringsRef;
  59. my @result = ();
  60. push(@result, "#ifndef NDEBUG\n");
  61. while ( my ($name, $value) = each %strings ) {
  62. push(@result, " ${name}Impl->assertHashIsCorrect();\n");
  63. }
  64. push(@result, "#endif // NDEBUG\n");
  65. push(@result, "\n");
  66. return join "", @result;
  67. }
  68. 1;